Click to See Complete Forum and Search --> : Form validation


dhpotter
06-25-2003, 07:57 AM
Hi, I need to perform some form validation
ie make sure the fields dont contain spaces..

The following is the original script

function checkform(){
if (counter<5){
alert("Please choose the 4 bottles");
return false
}
else
return true;
}

this line creates the input box

<td ALIGN="right" height="46" width="259"><font color="#3366FF" face="Arial, Helvetica, sans-serif" size="4"> Name
:</font></td>
<td height="46" width="334"><input NAME="Name" SIZE="29"></td>

how do i reference the name box so that i can check it has a value.
i have tried the following

function checkform(){
if (counter<4){
alert("Please choose 4 bottles");
return false

if (this.form.Name ==""){
alert ("Please complete your personal details");
return false
}
else
return true;
}}

obviously I would need to check for all the fields , ie name, add, tel, email etc..
Can anyone help with the reference of the input box so I can check it..
any help would be great thanks
David

pyro
06-25-2003, 08:11 AM
To test for a white space character, you just do a simple regexp test:

<html>
<head>

<script type="text/javascript">
function validate(frm) {
if (/\s/.test(frm.mytext.value)) {
alert ("Contains whitespace");
return false;
}
else {
return true;
}
}
</script>

</head>
<body>
<form name="myform" onsubmit="return validate(this);">
<input type="text" name="mytext">
<input type="submit">
</form>
</body>
</html>