Can anyone see what is wrong with my code here, I am simply trying to validate form input in the following ways:
""JavaScript Data Validation of Visitor's name field Only
must be validated as follows:
a) the length of the input is bigger than 0 and less/equal to 14;
b) only letters a to z (lower case), "-" (dash) and " " (space) are allowed,
c) the "-" (dash) and " " (space) letters must be entered,
d) the "-" or the " " letter must not be either the first or the last letter entered,
e) "-" must not be the immediate neighbour or adjacent (before or after) to a " ",
f) "-" or " " must not be the immediate neighbour (adjacent) of itself."
The script part:
<script language="javascript" type="text/javascript">
function checkinput(n)
{
var names = document.interestform.name.value;
var len = names.length;
var symbols = /\!\"\£\$\%\^\&\*\(\)\_\+\{\}\:\@\~\?\>\<\|\¬\`\=\[\]\;\#\,\.\/\¦/;
var nameformat = /^.+ .+-.+$/;
var telno = document.interestform.telno.value;
var telnoformat = /^.+{4,6} .+{7}$/;
if ((names.length == 0) || (names.length > 14))
{
alert ("Name must be no more than 14 characters long.");
return false;
}
else
{
return n;
}
if ((names.toLowerCase() !== names) && (names.match(symbols)))
{
alert ("Name must be entered in lower case with no");
return false;
}
else
{
return n;
}
if (!(names.test(nameformat)))
{
alert ("The format for Name must be first name followed by second name with a '-' in the middle of the second name.");
return false;
}
else
{
return n;
}
if (!(telno.test(telnoformat)))
{
alert ("Telephone number must be area 4-digit area code in brackets followed by a space and then 7 more digits. e.g. (1234) 1122334");
return false;
}
else
{
return n;
}
}
</script>
The form part:
<div id="main">
<h3>Interest Form</h3> <!-- make header -->
<form name="interestform" action="thankyou.html" method="post">
<table border=0 width="300px" cellpadding=4>
<tr>
<td>Name:<sup class="condition">*</sup><br><sub>E.g (Firstname Surname)</sub></td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>Tel. Number:<sup class="condition">*</sup><br><sub>E.g (0123) 1122334</sub></td>
<td><input type="text" name="telno"></td>
</tr>
<tr>
<td>Location:<sup class="condition">*</sup></td>
<td>
<select name="location" value="default">
<option value="default">Select Location</option>
<option value="syorks">South Yorkshire</option>
<option value="england">Rest of England</option>
<option value="wales">Wales</option>
<option value="scotland">Scotland</option>
<option value="ireland">Ireland</option>
<option value="eu">European Union</option>
<option value="restofworld">Rest of the World</option>
<option value="other">Other</option>
</select>
</td>
</tr>
<tr>
<td>Email:<br><sub>E.g. (abc@hotmail.com)</sub></td>
<td><input type="text" name="email" value=""></td>
</tr>
<tr>
<td><sup class="condition">* Denotes a required field.</sup></td>
<td></td>
</tr>
<tr>
<td><input type="submit" value="Submit" onClick="checkinput(n)"></td>
<td><input type="reset" value="Reset"></td>
</tr>
</table>
</form>
</div>
Any help would be appreciated, I have spent hours fiddling with it trying to find an error or a way to get it to work, but I cannot see anything wrong with it, im afraid my Javascript knowledge is very basic.