Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    May 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    validation help.

    Hello.

    I'm using javascript to try and validate a form so that it only submits if the first of the two student number fields is numeric, 8 digits long and that the two copies of the student number are the same. I am not sure how to test this yet because I havent made the JSP page yet. This is the first time ive used javascript and was wondering if you could tell me whether this looks correct?

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    
    <script type="text/javascript">
     function isNumeric (Password1) {
     
      var v = Password1.value;
      for (var i = 0; i < v.length; i++) {
        var c = v.charAt(i);
        if (c < '0' || c > '9') {
          alert(v + " is not a number!")
          return false;
        }
      }
      
      if (v.length == '8'){
      return true;
      }
    }
    </script>
    
    <style type="text/css">
    <!--
    .originalForm {  width: 560px;
      background-color: #99FFCC;
      border: 1px dotted #333;
      padding: 5px;
      margin: 0px auto;
      padding-bottom: 20px;
    }
    -->
    </style>
    </head>
    
    <body>
    
    <h1>University Society Registration Form</h1>
     
    
    <form id="form1" name="form1" method="get" action="http://itsuite.it.brighton.ac.uk:9090/je/test.jsp"  onsubmit="return isNumeric(Password1)">
    
    <label>Name
      <input type="text" name="Username" id="Username" />
      </label>
      <br />
      <br />
       <label>Email
       <input type="text" name="Email" id="Email" />
    </label>
      <br />
      <br />
       <label>Student Number:
      <input type="password" name="password1" id="password1" />
      </label>
      <br />
      <br />
     <label>Repeat Student Number:
      <input type="password" name="password2" id="password2" />
    </label>
    
    
     <p class="submit"><span class="originalForm">
       <input type="submit" name="submit" id="submit" value="Submit" />
     </span></p>
     <p>&nbsp;</p>
    </form>
    
    </body>
    </html>
    thank you in advance.

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    You'll need to be more careful about how you reference your input fields-
    here is one way:
    Code:
    function inputmatch(){
    	var p1= document.getElementsByName('password1')[0].value;
    	var p2= document.getElementsByName('password2')[0].value;
    	if(/^\d{8}$/.test(p1)){
    		if(p1!= p2){
    			alert('Both numbers must match');
    		}
    		else return true;
    	}
    	else alert('Must be an 8 digit number');
    	return false;
    }


  •  

    Posting Permissions

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