Need help with JavaScript validation for multiple forms

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kktnguyen
    New Member
    • Feb 2007
    • 4

    Need help with JavaScript validation for multiple forms

    Hello, Please help me with this code..I have 4 forms which link to different htmll page. I try to write one validation file that can validate for the form whenever user click on that form.How do I call Validate.js for each form? Please give me some hints.
    Thanks

    Validate.js
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //test version
    //read the comment i put
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////
    function do_checkEmail(x ){
    //do all email checking
    if(x.value.inde xOf("@")!= "-1" && x.value.indexOf (".") != "-1"){
    return false;
    }
    else
    {
    return true;

    }
    }
    function do_checkPhone(x ){
    //do all phone checking phone format 999-999-9999
    if(x.value.leng th !=12){
    return false;
    }

    for(var i = 0; i < x.value.length; i++){
    if ((i>-1&&i<3)||(i>3 && i<7)||(i>7&& i>12)){
    if(x.value.char At(i) < "0" || x.value.charAt( i) > "9"){
    return false;
    }
    }
    //check for hyphen '-'
    else if (x.value.charAt (i) != "-"){
    return false;
    }
    }
    return true;
    }
    //Check for empty field, last name and first name
    function do_checkEmpty(x ){
    if (x.value == "" || x.value == "null"){
    return false;
    }
    else{
    return true;
    }

    }

    function checkForm(form) {

    if (do_checkEmail( form.emailAdd) == false){
    alert("Please enter a valid email.");
    form.emailAdd.f ocus();
    return false;
    }
    if (do_checkEmpty( form.lastname) == false){
    alert("Please Enter Last Name.");
    form.lastname.f ocus();
    return false;
    }
    if (do_checkEmpty( form.firstname) == false){
    alert("Please Enter First Name.");
    form.firstname. focus();
    return false;
    }
    return true;

    }
    //////////////////////////////////////////////////////////////////////
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #2
    plz send ur html code ....

    i am online ...

    Comment

    • kktnguyen
      New Member
      • Feb 2007
      • 4

      #3
      Originally posted by dmjpro
      plz send ur html code ....

      i am online ...
      Thank for your help
      here are the UR for two form
      http://cs.metrostate.e du/~ics499sp0702/Khuong/Info.html
      http://cs.metrostate.e du/~ics499sp0702/Khuong/Customer.html

      Comment

      • dmjpro
        Top Contributor
        • Jan 2007
        • 2476

        #4
        i don't understand how u link two html pages..
        i think u are showing the pages in seperate windows

        add a function in js ...

        function myCheck(form_ob j)
        {
        if(checkForm(fo rm_obj)) document.forms[0].submit();
        }

        and edit ...

        checkForm(form) {

        if (do_checkEmail( form.emailAdd) == false){
        alert("Please enter a valid email.");
        form.emailAdd.f ocus();
        return false;
        }
        if (do_checkEmpty( form.lastname) == false){
        alert("Please Enter Last Name.");
        form.lastname.f ocus();
        return false;
        }
        if (do_checkEmpty( form.firstname) == false){
        alert("Please Enter First Name.");
        form.firstname. focus();
        return false;
        }
        return true;

        }

        before validate ...
        check the existence of that object ..

        if (typeof form.email != 'undefined' && Adddo_checkEmai l(form.emailAdd ) == false){
        alert("Please enter a valid email.");
        form.emailAdd.f ocus();
        return false;
        }

        now try urself ... best of luck

        Comment

        • kktnguyen
          New Member
          • Feb 2007
          • 4

          #5
          Thank you much I will give it a try.

          k

          Comment

          Working...