A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: Java validation help

  1. #1
    Senior Member
    Join Date
    Mar 2002
    Posts
    172

    Java validation help

    hi...I'm trying to validate phone number (correct format), name (only letters and minimun 2 letter) and DOB (Correct format, no letters). What I have so far only returns false in the user leaves the field "Empty". Any help would be greatly appreciated, thx!:

    <script type="text/javascript">

    <!--

    function validate_form ( )
    {
    valid = true;

    if ( document.schedule.firstname.value == "" )
    {
    alert ( "Please fill in your 'First Name'" );
    valid = false;
    }

    else if ( document.schedule.lastname.value == "" )
    {
    alert ( "Please fill in your 'Last Name'" );
    document.schedule.lastname.focus();
    valid = false;
    }

    else if ( document.schedule.phone.value == "" )
    {
    alert ( "Please fill in your 'Phone Number' and remember to include your area code" );
    document.schedule.phone.focus();
    valid = false;
    }

    else if ( document.schedule.phone2.value == "" )
    {
    alert ( "Please fill in your 'Phone Number' and remember to include your area code" );
    document.schedule.phone2.focus();
    valid = false;
    }

    else if ( document.schedule.phone3.value == "" )
    {
    alert ( "Please fill in your 'Phone Number' and remember to include your area code" );
    document.schedule.phone3.focus();
    valid = false;
    }

    else if ( document.schedule.dob.value == "" )
    {
    alert ( "Please fill in your 'Date of Birth'. Your birth date must be in the past." );
    document.schedule.dob.focus();
    valid = false;
    }

    else if ( ( document.schedule.referral[0].checked == false ) && ( document.schedule.referral[1].checked == false ) )
    {
    alert ( "Do you have a referral from your doctor?" );
    valid = false;
    }

    else if ( ( document.schedule.hmo[0].checked == false ) && ( document.schedule.hmo[1].checked == false ) )
    {
    alert ( "Is your insurance in an Health Maintenance Organization (HMO)?" );
    valid = false;
    }

    else if ( ( document.schedule.hmo[0].checked == false ) && ( document.schedule.hmo[1].checked == true ) )
    {
    alert ( "HMO Insurance plans require an authorization. Please contact your MD to obtain this, and come back to our website to request your appointment." );
    valid = false;
    }


    return valid;
    }

    //-->

    </script>
    Where is Ralph Machio these days...?

  2. #2
    Senior Member catbert303's Avatar
    Join Date
    Aug 2001
    Location
    uk
    Posts
    11,222
    Regular expressions could be useful here.

    Here's a couple of quick examples,

    Code:
    if (!document.schedule.firstname.match(/^[a-z]{2,}$/i)) {
      // the regular expression checks to see if the string is 2 letters or longer and only 
      // contains characters a-z. if the firstname doesn't match that pattern we end up here
    }
    
    if (!document.schedule.dob.value.match(/^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/)) {
      // this one checks if the date of birth was in the format 2 digits/2 digits/4 digits
      // if not we end up in here
    }
    Though you may want to improve this to be more accomodating for different values in the name. for example many people might have spaces, -, or ' in their name. Not to mention people whose names require letters outside of US-ASCII

    http://www.regular-expressions.info/javascript.html has far more information.

  3. #3
    Senior Member
    Join Date
    Mar 2002
    Posts
    172
    Thanks for your reply! Please check the below......what am I doing wrong? It still let's the user get away with single letter as firstname? And it submits the form after the first validator and doesn't continue to the other else if validators. What am I missing?

    Thank you...

    function validate_form ( )
    {
    valid = true;

    if ( document.schedule.firstname.value == "" )
    {
    alert ( "Please fill in your 'First Name'" );
    valid = false;
    }

    else if (!document.schedule.firstname.match(/^[a-z]{2,}$/i)) {

    alert ( "Your first name seems to be incorrect" );
    document.schedule.firstname.focus();
    valid = false;
    }

    else if ( document.schedule.lastname.value == "" )
    {
    alert ( "Please fill in your 'Last Name'" );
    document.schedule.lastname.focus();
    valid = false;
    }

    else if ( document.schedule.phone.value == "" )
    {
    alert ( "Please fill in your 'Phone Number' and remember to include your area code" );
    document.schedule.phone.focus();
    valid = false;
    }

    else if ( document.schedule.phone2.value == "" )
    {
    alert ( "Please fill in your 'Phone Number' and remember to include your area code" );
    document.schedule.phone2.focus();
    valid = false;
    }

    else if ( document.schedule.phone3.value == "" )
    {
    alert ( "Please fill in your 'Phone Number' and remember to include your area code" );
    document.schedule.phone3.focus();
    valid = false;
    }

    else if ( document.schedule.dob.value == "" )
    {
    alert ( "Please fill in your 'Date of Birth'. Your birth date must be in the past." );
    document.schedule.dob.focus();
    valid = false;
    }

    else if ( ( document.schedule.referral[0].checked == false ) && ( document.schedule.referral[1].checked == false ) )
    {
    alert ( "Do you have a referral from your doctor?" );
    valid = false;
    }

    else if ( ( document.schedule.hmo[0].checked == false ) && ( document.schedule.hmo[1].checked == false ) )
    {
    alert ( "Is your insurance in an Health Maintenance Organization (HMO)?" );
    valid = false;
    }

    else if ( ( document.schedule.hmo[0].checked == false ) && ( document.schedule.hmo[1].checked == true ) )
    {
    alert ( "HMO Insurance plans require an authorization. Please contact your MD to obtain this, and come back to our website to request your appointment." );
    valid = false;
    }


    return valid;
    }
    Where is Ralph Machio these days...?

  4. #4
    Senior Member catbert303's Avatar
    Join Date
    Aug 2001
    Location
    uk
    Posts
    11,222
    Sorry, I missed the .value part out when reading the values from the form fields.

    document.schedule.firstname.value.match(...

    and so on for the other fields

  5. #5
    Senior Member
    Join Date
    Mar 2002
    Posts
    172
    Thank you so much for all your help! I was able to modify your dob code to validate the phone number as well.

    Working ex. http://206.169.68.234/schedule/sched...aphy_hbc2.html

    My last question would be that many error boxes keeps popping up in a row? 3 or four sometimes....how can I avoid this....check above link.

    Below is the code....thanks in advance:

    <script language="JavaScript">

    function validate_form ( )
    {
    valid = true;

    if ( document.schedule.firstname.value == "" )
    {
    alert ( "Please fill in your 'First Name'" );
    valid = false;
    }

    else if (!document.schedule.firstname.value.match(/^[a-z]{2,}$/i)) {

    alert ( "Your first name seems to be incorrect" );
    document.schedule.firstname.focus();
    valid = false;
    }

    else if ( document.schedule.lastname.value == "" )
    {
    alert ( "Please fill in your 'Last Name'" );
    document.schedule.lastname.focus();
    valid = false;
    }

    else if (!document.schedule.lastname.value.match(/^[a-z]{2,}$/i)) {

    alert ( "Your last name seems to be incorrect" );
    document.schedule.lastname.focus();
    valid = false;
    }

    else if ( document.schedule.phone.value == "" )
    {
    alert ( "Please fill in your 'Phone Number' and remember to include your area code" );
    document.schedule.phone.focus();
    valid = false;
    }

    if (!document.schedule.phone.value.match(/^[0-9]{3}$/)) {
    alert ( "Phone number must xxx-xxx-xxxx" );
    document.schedule.phone.focus();
    valid = false;
    }

    else if ( document.schedule.phone2.value == "" )
    {
    alert ( "Please fill in your 'Phone Number' and remember to include your area code" );
    document.schedule.phone2.focus();
    valid = false;
    }

    if (!document.schedule.phone2.value.match(/^[0-9]{3}$/)) {
    alert ( "Phone number must xxx-xxx-xxxx" );
    document.schedule.phone2.focus();
    valid = false;
    }

    else if ( document.schedule.phone3.value == "" )
    {
    alert ( "Please fill in your 'Phone Number' and remember to include your area code" );
    document.schedule.phone3.focus();
    valid = false;
    }

    if (!document.schedule.phone3.value.match(/^[0-9]{4}$/)) {
    alert ( "Phone number must xxx-xxx-xxxx" );
    document.schedule.phone3.focus();
    valid = false;
    }

    else if ( document.schedule.dob.value == "" )
    {
    alert ( "Please fill in your 'Date of Birth'. Your birth date must be in the past." );
    document.schedule.dob.focus();
    valid = false;
    }

    if (!document.schedule.dob.value.match(/^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/)) {
    alert ( "Date format must be mm/dd/yyyy" );
    document.schedule.dob.focus();
    valid = false;
    }

    else if ( ( document.schedule.referral[0].checked == false ) && ( document.schedule.referral[1].checked == false ) )
    {
    alert ( "Do you have a referral from your doctor?" );
    valid = false;
    }

    else if ( ( document.schedule.hmo[0].checked == false ) && ( document.schedule.hmo[1].checked == false ) )
    {
    alert ( "Is your insurance in an Health Maintenance Organization (HMO)?" );
    valid = false;
    }

    else if ( ( document.schedule.hmo[0].checked == false ) && ( document.schedule.hmo[1].checked == true ) )
    {
    alert ( "HMO Insurance plans require an authorization. Please contact your MD to obtain this, and come back to our website to request your appointment." );
    valid = false;
    }


    return valid;
    }

    // function formSubmit()
    //{
    // returnValue = false;
    // if (verifyForm())

    returnValue = true;

    // if (verifyForm()) //check the form by calling your checker function
    // it will return true if the form is good
    // returnValue = true;


    // return returnValue;


    //}
    </script>
    Where is Ralph Machio these days...?

  6. #6
    Senior Member
    Join Date
    Mar 2002
    Posts
    172
    and finally....since the script would be used in over 30 forms is there a way to make it external for easier editing...sorry for my ignorance!

    Thx,

    Mik
    Where is Ralph Machio these days...?

  7. #7
    Senior Member catbert303's Avatar
    Join Date
    Aug 2001
    Location
    uk
    Posts
    11,222
    Hi,

    If you only want at most one error message to be displayed when the form is submitted you need to make sure you only have one if statement in the validation, and that every other check is done using an else if branch.

    As for making the script external, yes, that can be done. Take the content from your script tags (make sure not to include the tags themselves) and place in in a separate file. Save this as something like validation.js, then from your page you can include this file using,

    <script type="text/javascript" src="validation.js"> </script>

  8. #8
    Senior Member
    Join Date
    Mar 2002
    Posts
    172
    Thank you so much for all your help.....

    http://206.169.68.234/schedule/sched...raphy_hbc.html

    Mik
    Where is Ralph Machio these days...?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center