CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript programming (http://www.codingforums.com/forumdisplay.php?f=2)
-   -   Javascript validation problems (http://www.codingforums.com/showthread.php?t=211446)

Daphatboy 12-08-2010 08:05 PM

Javascript validation problems
 
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 :)

Philip M 12-08-2010 08:34 PM

When posting here please help us to help you by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.

Have you tried to identify syntax errors with your error console or better still Firebug?

Your UK postcode validator can be simplified and improved with a regex:-

if(/((^(A[BL]|B[ABDHLNRST]|C[ABFHMORTVW]|D[ADEGHLNTY]|E[HNX]|F[KY]|G[LUY]|H[ADGPRSUX]|I[GMPV]|JE|K[ATWY]|L[ADELNSU]|M[EKL]|N[EGNPRW]|O[LX]|P[AEHLOR]|R[GHM]|S[AEGKL-PRSTWY]|T[ADFNQRSW]|UB|W[ADFNRSV]|YO|ZE)\d\d?)|(^W1[A-HJKSTUW0-9])|(^WC[1-2])|(^EC[1-4])|(^SW1[ABEHMNPRVWXY])|(^GIR\s?0AA))(\s\d[ABDEFGHJLNPQRSTUWXYZ]{2})$/.test(postcode.value)) {

NB: Letters C, I, K, M, O and V are never used in the incode. Postcode must be in upper-case.

Quizmaster: Which animal do we get vension from?
Contestant: Snakes.

Daphatboy 12-09-2010 11:03 AM

Oky, cheers for the help, unfortunately I have no idea how to use firebug :(
And how do I implement that piece of code? As basically the second time I have used meaningful javascript I'm not exactly compitent yet.

Many Thanks

Daphatboy 12-10-2010 12:10 PM

Can anyone help? sorry about the double post but I really need this sorted

Philip M 12-10-2010 12:32 PM

You are not calling your validation script anywhere.
<input type="submit" name="Submit" value="Submit" />
You could have found this out just by putting in an alert.
Code:

function formValidator(form){
alert ("So far so good");

Try this:-
Code:

<input type="submit" name="Submit" value="Submit" onsubmit = return formValidator(this.form){/>


All times are GMT +1. The time now is 03:50 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.