Validations not working!!!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Naha
    New Member
    • Feb 2007
    • 41

    #1

    Validations not working!!!

    Hi guys,

    I have a problem, can someone help me? I'm not sure why but none of my validation functions are working!!!!




    Code:
    <html>
    
    <head><title>Registration Details</title>
    
    <SCRIPT LANGUAGE="JavaScript">
    
    function processForm1() {
    
    form1 = document.forms["formOne"] 
    
    	if (form1.firstname.value==""){
    	alert("you must fill in your first name")
    
    	formOne.firstname.focus();
    
    	}
    
    	else if (form1.lastname.value==""){
    	alert("you must fill in your lastname")
    	formOne.lastname.focus();
    	}
    
    	else if (form1.postcode.value==""){
    	alert("you must fill in your postcode")
    	formOne.postcode.focus();
    	}
    
    	else if (form1.dateofbirth.value==""){
    	alert("you must fill in your DOB")
    	formOne.dateofbirth.focus();
    	}
    
    	else { 
    	controlForm = parent.frames[1].document.controlForm
    	controlForm.firstname.value=form1.firstname.value
    	controlForm.lastname.value=form1.lastname.value
    	controlForm.Address.value=form1.Address.value
    	controlForm.city.value=form1.city.value
    	controlForm.postcode.value=form1.postcode.value
    	controlForm.dateofbirth.value=form1.dateofbirth.value
    	controlForm.contactphonenumber.value=form1.contactphonenumber.value
    	controlForm.emailaddress.value=form1.emailaddress.value
    	controlForm.mypassword.value=form1.mypassword.value 
    	}
    } 
    
    
     function PhoneNumber{
    
     	if (form1.checkphonenumber.value==""){
    	alert("you must fill in your contactphonenumber")
    	formOne.checkphonenumber.focus();
    	}
    
    	var stripped = checkphonenumber.replace(/[\(\)\.\-\ ]/g, '');
    	//strip out acceptable non-numeric characters
    
    	else if (isNaN(parseInt(stripped))) {
      	 alert("the phone number contains illegal characters")}
    
    
    	else if (!(stripped.length == 11)) {
    	alert("The phone number is the wrong length. Make sure you included an area code.")}
    	
    	}
    
    
    function checkEmail{
    
    	var emailFilter=/^.+@.+\..{2,3,4,6}$/;
    
     	if (form1.emailaddress.value==""){
    	alert("you must fill in your email address")
    	formOne.emailaddress.focus();
    	}
    
    	else if (!(emailFilter.test(emailaddress))) { 
          	alert("Enter a valid email add")
    	}
    
    	else if (form1.mypassword.value==""){
    	alert("you must fill in your password")
    	formOne.mypassword.focus();}
    	
    	}
    
    function checkPassword{
    
    
    	var illegalChars = /\W/;
    
     	if ((mypassword.length < 6) || (mypassword.length > 8)) {
           	alert("Password should have atleast 6 characters but no more than 7.")
    	formOne.mypassword.focus();
    	}
    
       	else if (illegalChars.test(mypassword)) {
           	alert("The password can only contain letters and numbers")} 
    	
    	
    	
    	}
    
    </script>
    </head>
    
    
    <body>
    
    <form method="GET" action="http://localhost:8081/MyServlet" NAME="formOne" >
    
    
    <center>
    
    <h1>Registration Details</h1>
    
    <br><br>
    
    First Name <input type="text" name="firstname" value=""><br><br>
    
    Last Name <input type="text" name="lastname" value=""><br><br>
    
    Address <input type="text" name="address" value=""><br><br>
    
    City <input type="text" name="city" value=""><br><br>
    
    County (optional) <input type="text" name="county" value=""><br><br>
    
    Post Code <input type="text" name="postcode" value=""><br><br>
    
    Country <input type="text" name="country" value=""><br><br>
    
    Contact Phone Number <input type="text" name="contactphonenumber" value=""><br><br>
    
    Date of Birth <input type="text" name="dateofbirth" value=""><br><br>
    
    Email Address <input type="text" name="emailaddress" value=""><br><br>
    
    Password (minimum 6 characters) <INPUT TYPE=PASSWORD NAME="mypassword"><br><br>
    
    <input type="BUTTON" NAME="Submit"  value="--SUBMIT--" onClick="processForm1()">
    
    </center>
    
    </form>
    
    </body>
    
    </html>
  • iam_clint
    Recognized Expert Top Contributor
    • Jul 2006
    • 1207

    #2
    Code:
    <html>
    <head><title>Registration Details</title>
    <SCRIPT LANGUAGE="JavaScript">
    function processForm1() {
    var form1 = document.forms.formOne
     if (form1.firstname.value==""){
     alert("you must fill in your first name")
     formOne.firstname.focus();
     }
     else if (form1.lastname.value==""){
     alert("you must fill in your lastname")
     formOne.lastname.focus();
     }
     else if (form1.postcode.value==""){
     alert("you must fill in your postcode")
     formOne.postcode.focus();
     }
     else if (form1.dateofbirth.value==""){
     alert("you must fill in your DOB")
     formOne.dateofbirth.focus();
     }
    } 
    
     function PhoneNumber() {
      if (form1.checkphonenumber.value==""){
     alert("you must fill in your contactphonenumber")
     formOne.checkphonenumber.focus();
     }
     var stripped = checkphonenumber.replace(/[\(\)\.\-\ ]/g, '');
     //strip out acceptable non-numeric characters
     if (isNaN(parseInt(stripped))) {
    	alert("the phone number contains illegal characters")}
    
     else if (!(stripped.length == 11)) {
     alert("The phone number is the wrong length. Make sure you included an area code.")}
     
     }
    
    function checkEmail() {
     var [email="emailFilter=/^.+@.+\..{2,3,4,6}$/"]emailFilter=/^.+@.+\..{2,3,4,6}$/[/email];
      if (form1.emailaddress.value==""){
     alert("you must fill in your email address")
     formOne.emailaddress.focus();
     }
     else if (!(emailFilter.test(emailaddress))) { 
    	   alert("Enter a valid email add")
     }
     else if (form1.mypassword.value==""){
     alert("you must fill in your password")
     formOne.mypassword.focus();}
     
     }
    function checkPassword() {
    
     var illegalChars = /\W/;
      if ((mypassword.length < 6) || (mypassword.length > 8)) {
    		alert("Password should have atleast 6 characters but no more than 7.")
     formOne.mypassword.focus();
     }
    	else if (illegalChars.test(mypassword)) {
    		alert("The password can only contain letters and numbers")} 
     
     
     
     }
    </script>
    </head>
    
    <body>
    <form method="GET" action="[url="http://localhost:8081/MyServlet"]http://localhost:8081/MyServlet[/url]" NAME="formOne" >
    
    <center>
    <h1>Registration Details</h1>
    <br><br>
    First Name <input type="text" name="firstname" value=""><br><br>
    Last Name <input type="text" name="lastname" value=""><br><br>
    Address <input type="text" name="address" value=""><br><br>
    City <input type="text" name="city" value=""><br><br>
    County (optional) <input type="text" name="county" value=""><br><br>
    Post Code <input type="text" name="postcode" value=""><br><br>
    Country <input type="text" name="country" value=""><br><br>
    Contact Phone Number <input type="text" name="contactphonenumber" value=""><br><br>
    Date of Birth <input type="text" name="dateofbirth" value=""><br><br>
    Email Address <input type="text" name="emailaddress" value=""><br><br>
    Password (minimum 6 characters) <INPUT TYPE=PASSWORD NAME="mypassword"><br><br>
    <input type="BUTTON" NAME="Submit"  value="--SUBMIT--" onClick="processForm1()">
    </center>
    </form>
    </body>
    </html>
    give this a try

    Comment

    • Naha
      New Member
      • Feb 2007
      • 41

      #3
      With the code you gave only the first function works the others do not.



      Code:
      <html>
      <head><title>Registration Details</title>
      <SCRIPT LANGUAGE="JavaScript">
      function processForm1() {
      var form1 = document.forms.formOne
       if (form1.firstname.value==""){
       alert("you must fill in your first name")
       formOne.firstname.focus();
       }
       else if (form1.lastname.value==""){
       alert("you must fill in your lastname")
       formOne.lastname.focus();
       }
       else if (form1.postcode.value==""){
       alert("you must fill in your postcode")
       formOne.postcode.focus();
       }
       else if (form1.dateofbirth.value==""){
       alert("you must fill in your DOB")
       formOne.dateofbirth.focus();
       }
      } 
      
       function PhoneNumber() {
        if (form1.checkphonenumber.value==""){
       alert("you must fill in your contactphonenumber")
       formOne.checkphonenumber.focus();
       }
       var stripped = checkphonenumber.replace(/[\(\)\.\-\ ]/g, '');
       //strip out acceptable non-numeric characters
       if (isNaN(parseInt(stripped))) {
      	alert("the phone number contains illegal characters")}
      
       else if (!(stripped.length == 11)) {
       alert("The phone number is the wrong length. Make sure you included an area code.")}
       
       }
      
      function checkEmail() {
       var [email="emailFilter=/^.+@.+\..{2,3,4,6}$/"]emailFilter=/^.+@.+\..{2,3,4,6}$/[/email];
        if (form1.emailaddress.value==""){
       alert("you must fill in your email address")
       formOne.emailaddress.focus();
       }
       else if (!(emailFilter.test(emailaddress))) { 
      	   alert("Enter a valid email add")
       }
       else if (form1.mypassword.value==""){
       alert("you must fill in your password")
       formOne.mypassword.focus();}
       
       }
      function checkPassword() {
      
       var illegalChars = /\W/;
        if ((mypassword.length < 6) || (mypassword.length > 8)) {
      		alert("Password should have atleast 6 characters but no more than 7.")
       formOne.mypassword.focus();
       }
      	else if (illegalChars.test(mypassword)) {
      		alert("The password can only contain letters and numbers")} 
       
       
       
       }
      </script>
      </head>
      
      <body>
      <form method="GET" action="[url="http://localhost:8081/MyServlet"]http://localhost:8081/MyServlet[/url]" NAME="formOne" >
      
      <center>
      <h1>Registration Details</h1>
      <br><br>
      First Name <input type="text" name="firstname" value=""><br><br>
      Last Name <input type="text" name="lastname" value=""><br><br>
      Address <input type="text" name="address" value=""><br><br>
      City <input type="text" name="city" value=""><br><br>
      County (optional) <input type="text" name="county" value=""><br><br>
      Post Code <input type="text" name="postcode" value=""><br><br>
      Country <input type="text" name="country" value=""><br><br>
      Contact Phone Number <input type="text" name="contactphonenumber" value=""><br><br>
      Date of Birth <input type="text" name="dateofbirth" value=""><br><br>
      Email Address <input type="text" name="emailaddress" value=""><br><br>
      Password (minimum 6 characters) <INPUT TYPE=PASSWORD NAME="mypassword"><br><br>
      <input type="BUTTON" NAME="Submit"  value="--SUBMIT--" onClick="processForm1()">
      </center>
      </form>
      </body>
      </html>
      give this a try[/QUOTE]

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Originally posted by Naha
        With the code you gave only the first function works the others do not.
        Because you haven't defined form1 to be the form. Add the following (as in the first function) to the beginning of the remaining functions:
        Code:
        var form1 = document.forms.formOne;

        Comment

        • Naha
          New Member
          • Feb 2007
          • 41

          #5
          no, the remaining functions still are not working.

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            You need to use one or the other: form1 or formOne (whichever one you have defined). Don't use them interchangeably .

            Use
            Code:
            var form1 = document.formOne;
            instead of what you currently have. Currently your focus code for each if branch is incorrect - it is using formOne, but you haven't defined it.

            Similarly, you are accessing fields improperly. You have to access them through the form object, e.g.
            Code:
            var mypassword = form1.mypassword;
            where form1 is the form object. Now you can use mypassword.

            Comment

            • Naha
              New Member
              • Feb 2007
              • 41

              #7
              I made the changes but its still no working.





              Code:
              <html>
              <head><title>Registration Details</title>
              <SCRIPT LANGUAGE="JavaScript">
              
              function processForm1() {
              var form1 = document.formOne;
               if (form1.firstname.value==""){
               alert("you must fill in your first name")
               formOne.firstname.focus();
               }
               else if (form1.lastname.value==""){
               alert("you must fill in your lastname")
               formOne.lastname.focus();
               }
               else if (form1.postcode.value==""){
               alert("you must fill in your postcode")
               formOne.postcode.focus();
               }
               else if (form1.dateofbirth.value==""){
               alert("you must fill in your DOB")
               formOne.dateofbirth.focus();
               }
               
              function checkPhoneNumber() {
              var form1 = document.formOne;
              var contactphonenumber = form1.contactphonenumber;
                if (form1.contactphonenumber.value==""){
               alert("you must fill in your contactphonenumber")
               formOne.contactphonenumber.focus();
               }
               var stripped = contactphonenumber.replace(/[\(\)\.\-\ ]/g, '');
               //strip out acceptable non-numeric characters
               if (isNaN(parseInt(stripped))) {
              	alert("the phone number contains illegal characters")}
              
               else if (!(stripped.length == 11)) {
               alert("The phone number is the wrong length. Make sure you included an area code.")}
               
              }
              
              function checkEmail() {
              var form1 = document.formOne;
              var emailaddress = form1.emailaddress;
              var emailFilter=/^/w+([/.-]?/w+)*@;
                if (form1.emailaddress.value==""){
               alert("you must fill in your email address")
               formOne.emailaddress.focus();
               }
               else if (!(emailFilter.test(emailaddress)))
              {
              	   alert("Enter a valid email add")
              		 formOne.emailaddress.focus();
               }
               
               }
              
              function checkPassword() {
              var form1 = document.formOne;
              var mypassword = form1.mypassword;
               var illegalChars = /\W/;
                if ((mypassword.length < 6) || (mypassword.length > 8)) {
              		alert("Password should have atleast 6 characters but no more than 7.")
               formOne.mypassword.focus();
               }
              	else if (illegalChars.test(mypassword)) {
              		alert("The password can only contain letters and numbers")} 
               
               }
               
              
              </script>
              </head>
              
              <body>
              <form method="GET" action="http://localhost:8081/MyServlet" NAME="formOne"    >
              
              <center>
              <h1>Registration Details</h1>
              <br><br>
              First Name <input type="text" name="firstname" value=""><br><br>
              Last Name <input type="text" name="lastname" value=""><br><br>
              Address <input type="text" name="address" value=""><br><br>
              City <input type="text" name="city" value=""><br><br>
              County (optional) <input type="text" name="county" value=""><br><br>
              Post Code <input type="text" name="postcode" value=""><br><br>
              Country <input type="text" name="country" value=""><br><br>
              Contact Phone Number <input type="text" name="contactphonenumber" value=""><br><br>
              Date of Birth <input type="text" name="dateofbirth" value=""><br><br>
              Email Address <input type="text" name="emailaddress" value=""><br><br>
              Password (minimum 6 characters) <INPUT TYPE=PASSWORD NAME="mypassword"><br><br>
              <input type="BUTTON" NAME="Submit"  value="--SUBMIT--" onClick="processForm1() && checkEmail() && checkPassword() && checkPhoneNumber()">
              </center>
              </form>
              </body>
              </html>

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                You've still not replaced formOne with form1, e.g. see below:
                Code:
                function processForm1() {
                var form1 = document.formOne;
                 if (form1.firstname.value==""){
                 alert("you must fill in your first name")
                 form1.firstname.focus();
                 }
                 else if (form1.lastname.value==""){
                 alert("you must fill in your lastname")
                 form1.lastname.focus();
                 }
                 else if (form1.postcode.value==""){
                 alert("you must fill in your postcode")
                 form1.postcode.focus();
                 }
                 else if (form1.dateofbirth.value==""){
                 alert("you must fill in your DOB")
                 form1.dateofbirth.focus();
                 }
                }
                Also, you didn't close the first function.

                Your email regular expression is incorrect - the forward slashes should be back slashes (\w instead of /w). Also see this page for a better reg.exp. for emails.

                Comment

                Working...