Jump to content

[SOLVED] Form validation, submitting before script finishes.


atrum

Recommended Posts

Hey guys,

 

I need a little bit of help with an odd problem when validating a form in javascript.

The issue is that the form submits before the javascript finishes validating all fields.

 

It will validate up to compairing the two emails fields. txt_email, and txt_email_c, then it skips all other fields and submits the form.

 

Here is my code.

 

function validate_email(field,alerttxt)
{
with (field)
    {
    apos=value.indexOf("@");
    dotpos=value.lastIndexOf(".");
    if (apos<1||dotpos-apos<2) 
      {alert(alerttxt);return false;}
    else {return true;}
    }
}

function checkemail(field1,field2,alerttxt) 
{
    with (field1,field2)
    {
        if (field1.value != field2.value)
        {alert(alerttxt);return false;}
        else {return true;}
    } 
}

function validate_required(field,alerttxt)
{
with (field)
    {
    if (value==null||value=="")
    {alert(alerttxt);return false;}
    else {return true;}

    }
}





  

function validate_form(thisform)
{
with(thisform)
    {
     if (validate_required(txt_firstname,"Firstname must be filled out!")==false)
     {txt_firstname.focus();return false;}
     
     if (validate_required(txt_lastname,"Lastname must be filled out!")==false)
     {txt_lastname.focus();return false;}
     
     if (validate_required(txt_email,"Email must be filled out!")==false)
     {txt_email.focus();return false;}
     
     if (validate_email(txt_email,"Not a valid email!")==false)
     {txt_email.focus();return false;}
            
     if (validate_required(txt_email_c,"Email must be confirmed!")==false)
     {txt_email_c.focus();return false;}
     
       if (checkemail(txt_email_c,txt_email,"Emails do not match!")==false)
     {txt_email_c.focus();return false;}
            
     if (validate_required(txt_dob,"Username must be filled out!")==false)
     {txt_birthdate.focus();return false;}
     
     if (validate_required(txt_password,"Password must be filled out!")==false)
     {txt_password.focus();return false;}
     
     if (validate_required(txt_password_c,"Password must be confirmed!")==false)
     {txt_password_c.focus();return false;}
    
     }
} 

 

any help any one can provide in trouble shooting this issue would be appreciated.

Hey guys, I acutally got it resolved.

 

I used a while() statement in the email comparison check instead of a with()

 

function validate_match(field1,field2,alerttxt) 
{
    while (field1,field2)
    {
        if (field1.value != field2.value)
        {alert(alerttxt);return false;}
        else {return true;}
    } 
}

if (validate_match(txt_email_c,txt_email,"Emails do not match!")==false)
     {txt_email.focus();return false;}

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.