Form Validation Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • AnnMarie

    Form Validation Problem

    Can someone let me know why my phone validation doesn't work within my
    validate function. It works alone, but not within this function like
    the other validations do.

    Phone is not required, but if the user enters a phone number it must
    be in the form of 555-555-5555.

    Also, my submit confirmation dialog box comes up first and
    continuously. I want it to appear last and once before final valid
    submission.

    Please let me know of a correct and better way of validating these
    required fields: name, company, email, and validating phone for
    correct format if user fills in phone number.

    The code is attached and and the link is below.
    Thanks.



    <script language="JavaS cript" type="text/javascript">
    <!--
    function validate(theFor m)
    {
    var validity = true; // assume valid

    if(frmComments. name.value=='' && validity == true)
    {
    alert('Your full name is required. Please enter your full
    name!');
    validity = false;
    frmComments.nam e.focus();
    event.returnVal ue=false;
    }

    if(frmComments. company.value== '' && validity == true)
    {
    alert('Your company name is required. Please enter the name of
    your company!');
    validity = false;
    frmComments.com pany.focus();
    event.returnVal ue=false;
    }

    var pattern1 = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4 })+$/;
    if (!pattern1.test (frmComments.em ail.value) && validity == true)
    {
    alert("Invalid E-mail Address! Please re-enter.")
    frmComments.ema il.focus();
    validity = false;
    return false;
    }

    if(frmComments. zip.value == "" )
    return true;
    else
    {
    var pattern2 = /\d{5}/;
    if(pattern2.tes t(frmComments.z ip.value))
    return true;
    else
    {
    if(!pattern2.te st(frmComments. zip.value) && validity == true)
    {
    alert("Invalid Zip Code! Must be in form 12345. Please
    re-enter.");       
    frmComments.zip .focus();
    validity = false;
    return false;
    }
    }
    }

    if(frmComments. phone.value == "")
    return true;
    else
    {
    var pattern3 = /\d{3}\-\d{3}\-\d{4}/;
    if(pattern3.tes t(frmComments.p hone.value))
    return true;
    else
    {
    if(!pattern3.te st(frmComments. phone.value)&& validity == true)
    {
    alert("Invalid Phone Number! Must be in form 555-555-5555.
    Please re-enter.");       
    frmComments.pho ne.focus();
    validity = false;
    return false;
    }
    }
    }

    }


    function formSubmit()
    {
    window.event.re turnValue = false;
    if ( confirm ( "Are you sure you want to submit?" ) )
    window.event.re turnValue = true;
    }

    function formReset()
    {
    window.event.re turnValue = false;
    if ( confirm( "Are you sure you want to reset?" ) )
    {
    frmComments.nam e.focus();
    window.event.re turnValue = true;
    }
    }
    // -->
    </script>

    <form id="frmComments "
    action="http://matrix.csis.pac e.edu/~f03-it604-s03/cgi-bin/comments.pl"
    method="post" onSubmit="retur n validate(this); " onReset="return
    formReset(this) ">


    <input type="submit" align="right" value="Submit" name="submit"
    onclick="formSu bmit()">
  • Lee

    #2
    Re: Form Validation Problem

    AnnMarie said:[color=blue]
    >
    >Can someone let me know why my phone validation doesn't work within my
    >validate function. It works alone, but not within this function like
    >the other validations do.[/color]

    I can see a couple of problems, but most of the code is
    unreadable. Please reformat it to fit in under 80
    columns and post it again.

    Comment

    Working...