Hey guys, im a bit of a loser with Javascript and need a bit of validation done but I have no idea what the problem is with my code, can anyone help?
Form:
Code:
<div id="Layer3">
<form id="form" name="form" method="post" onsubmit='return formValidator(form)' action="Candidate output.php">
<p class="style3"><u>Search for a Candidate </u></p>
<p class="style3">Candidate ID
<input type="text" name="CandidateID" />
</p>
<p class="style3">File
<select name="File">
<option>--Select--</option>
<option>To be deleted</option>
</p>
<p class="style3">Name
<input type="text" name="Name" />
</p>
<p class="style3">Current Job Title
<select name="Current_Job_title">
<option>--Select--</option>
<option>Accessories</option>
</select>
</p>
<p class="style3">Contact Telephone
<input type="text" name="Contact_Telephone" />
</p>
<p class="style3">Contact Email
<input type="text" name="Contact_Email" />
</p>
<p class="style3">Town
<input type="text" name="Town" />
</p>
<p class="style3">County
<select name="County">
<option>--Select--</option>
<option>Aberdeenshire</option>
</select>
</p>
<p class="style3">Postcode
<input type="text" name="Postcode" />
</p>
<p class="style3">Languages spoken
<input type="text" name="Languages_Spoken" />
</p>
<p class="style3">Previous Job Title
<select name="Job_Title" >
<option>--Select--</option>
<option>Accessories</option>
</select>
</p>
<p class="style3">Previous Employer
<input type="text" name="Employer" />
</p>
<p class="style3">Previous Employment Type
<select name="Employment_Type">
<option>--Select--</option>
<option>Manufacturer</option>
</select>
</p>
<p class="style3">Previous Department Type
<select name="Department_type">
<option>--Select--</option>
<option>Accounts</option>
</select>
</p>
<p class="style3">Order By
<select name="Order_By">
<option>--Select--</option>
<option>CandidateID</option>
</select>
<input type="checkbox" name="Desc" value="y" />
Decending (Ascending leave unchecked)
</p>
<p class="style3">
<input type="submit" name="Submit" value="Submit" />
</p>
</form>
</div>
Validation in the <Head>;
<script language="JavaScript" type='text/javascript'>
function formValidator(form){
// Make quick references to our fields
var CandidateID = document.getElementById('CandidateID');
var Name = document.getElementById('Name');
var Contact_T = document.getElementById('Contact_Telephone');
var Contact_E = document.getElementById('Contact_Email');
var Town = document.getElementById('Town');
var Postcode = document.getElementById('Postcode');
var Ls = document.getElementById('Languages_Spoken');
var P_Employer = document.getElementById('Previous_Employer');
/*http://www.tizag.com/javascriptT/javascriptform.php*/
// Check each input in the order that it appears in the form!
if(isNumeric(CandiateID, "Please enter numbers only")){
if(isAlphabet(Name, "Please enter letters only")){
if(isNumeric(Contact_T, "Please enter numbers only")){
if(lengthrestriction(Contact_T, 11)){
if(emailValidator(Contact_E, "Please enter a valid email address")){
if(isAlphabet(Town, "Please enter letters only")){
if(postit(Postcode, "Please enter a valid Postcode")){
if(isAlphabet(LS, "Please enter letters only")){
if(isAlphabet(P_Employer, "Please enter letters only")){
return true;
}
}
}
}
}
}
}
}
}
return false;
function isNumeric(elem, alerttxt){
var numericExpression = /^[0-9]+$/;
if(elem.value.match(numericExpression)){
return true;
}else{
alert(alerttxt);
elem.focus();
return false;
}
}
function isAlphabet(elem, alerttxt){
var alphaExp = /^[a-zA-Z]+$/;
if(elem.value.match(alphaExp)){
return true;
}else{
alert(alerttxt);
elem.focus();
return false;
}
}
function isAlphanumeric(elem, alerttxt){
var alphaExp = /^[0-9a-zA-Z]+$/;
if(elem.value.match(alphaExp)){
return true;
}else{
alert(alerttxt);
elem.focus();
return false;
}
}
function lengthRestriction(elem, max){
var uInput = elem.value;
if(uInput.length >= uInput.length <= max){
return true;
}else{
alert("Please enter between 0 and " +max+ " characters");
elem.focus();
return false;
}
}
function emailValidator(elem, alerttxt){
var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
if(elem.value.match(emailExp)){
return true;
}else{
alert(alerttxt);
elem.focus();
return false;
}
}
function postit(elem, alerttxt){ //check postcode format is valid
test = document.details.Postcode.value; size = test.length
test = test.toUpperCase(); //Change to uppercase
while (test.slice(0,1) == " ") //Strip leading spaces
{test = test.substr(1,size-1);size = test.length
}
while(test.slice(size-1,size)== " ") //Strip trailing spaces
{test = test.substr(0,size-1);size = test.length
}
document.details.Postcode.value = test; //write back to form field
if (size < 6 || size > 8){ //Code length rule
alert(test + " is not a valid postcode - wrong length");
document.details.Postcode.focus();
return false;
}
if (!(isNaN(test.charAt(0)))){ //leftmost character must be alpha character rule
alert(test + " is not a valid postcode - cannot start with a number");
document.details.Postcode.focus();
return false;
}
if (isNaN(test.charAt(size-3))){ //first character of inward code must be numeric rule
alert(test + " is not a valid postcode - alpha character in wrong position");
document.details.Postcode.focus();
return false;
}
if (!(isNaN(test.charAt(size-2)))){ //second character of inward code must be alpha rule
alert(test + " is not a valid postcode - number in wrong position");
document.details.Postcode.focus();
return false;
}
if (!(isNaN(test.charAt(size-1)))){ //third character of inward code must be alpha rule
alert(test + " is not a valid postcode - number in wrong position");
document.details.Postcode.focus();
return false;
}
if (!(test.charAt(size-4) == " ")){//space in position length-3 rule
alert(test + " is not a valid postcode - no space or space in wrong position");
document.details.Postcode.focus();
return false;
}
count1 = test.indexOf(" ");count2 = test.lastIndexOf(" ");
if (count1 != count2){//only one space rule
alert(test + " is not a valid postcode - only one space allowed");
document.details.Postcode.focus();
return false;
}
alert("Postcode Format OK");
return true;
}
</script>
Im sure there are a few syntax errors but any and all help is greatly appreciated