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
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;

  }

 

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.