Jump to content

Validating multiple form fields


highandwild

Recommended Posts

Hello everyone.

 

My first POST of many maybe as I've just found this forum and whilst I do try and sort problems out before I ask for help I need to avoid stress and frustration. 

 

I just need to validate certain fields on a form when the user presses the Submit button.

 

The individual validation functions below work fine but I do not know how to check the email address , alert if invalid and set focus onto the field but then check the fname if the email address is valid and so on.

 

Any ideas someone or is there a better way of doing this?

 

Thanks

 

Justin

 

 

function validate_form(thisform)

{

with (thisform)

  {

 

  if (validate_emailadd(emailadd,"Not a valid e-mail address!")==false)

    {emailadd.focus;return false;}

 

  if (validate_fname(fname,"Please enter your forename.")==false)

    {fname.focus();return false;}

 

  if (validate_sname(sname,"Please enter your surname.")==false)

    {sname.focus();return false;}

 

  }

 

}

 

 

function validate_fname(field,alerttxt)

{

with (field)

  {

  lenoftxt=value.length;

  if (lenoftxt=0)

    {alert(alerttxt);return false;}

  else {return true;}

  }

}

 

function validate_sname(field,alerttxt)

{

with (field)

  {

  lenoftxt=value.length;

  if (lenoftxt=0)

    {alert(alerttxt);return false;}

  else {return true;}

  }

}

 

function validate_emailadd(field,alerttxt)

{

with (field)

  {

  apos=value.indexOf("@");

  dotpos=value.lastIndexOf(".");

  if (apos<1||dotpos-apos<2)

    {alert(alerttxt);return false;}

  else {return true;}

  }

}

 

 

Link to comment
https://forums.phpfreaks.com/topic/224300-validating-multiple-form-fields/
Share on other sites

You'll have to hold off on returning a result from the function until all processes are complete. 

 

Example:

 

function validate_form(thisform) {

var result = true;

with (thisform)

  {

 

  if (validate_emailadd(emailadd,"Not a valid e-mail address!")==false)

    {emailadd.focus;result = false;}

 

  if (validate_fname(fname,"Please enter your forename.")==false)

    {fname.focus();result = false;}

 

  if (validate_sname(sname,"Please enter your surname.")==false)

    {sname.focus();result = false;}

 

  return result;

  }

 

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.