View Full Version : validation help.

05-02-2008, 09:38 PM

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?

<!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">
<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;

<style type="text/css">
.originalForm { width: 560px;
background-color: #99FFCC;
border: 1px dotted #333;
padding: 5px;
margin: 0px auto;
padding-bottom: 20px;


<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)">

<input type="text" name="Username" id="Username" />
<br />
<br />
<input type="text" name="Email" id="Email" />
<br />
<br />
<label>Student Number:
<input type="password" name="password1" id="password1" />
<br />
<br />
<label>Repeat Student Number:
<input type="password" name="password2" id="password2" />

<p class="submit"><span class="originalForm">
<input type="submit" name="submit" id="submit" value="Submit" />


thank you in advance.

05-03-2008, 04:58 AM
You'll need to be more careful about how you reference your input fields-
here is one way:

function inputmatch(){
var p1= document.getElementsByName('password1')[0].value;
var p2= document.getElementsByName('password2')[0].value;
if(p1!= p2){
alert('Both numbers must match');
else return true;
else alert('Must be an 8 digit number');
return false;