943,164 Members | Top Members by Rank

Ad:
Jan 27th, 2010
0

Trying to hack a Javascript to validate a form on just a few variables in one field

Expand Post »
OK, I have a JS which validates the correct format for postcodes (or zip) on an html form - works fine.

But what I want to do is to use a JS to see if a website visitor is eligible for a particular service on their postcode / zip - ie the first general part on the code. For example if they type L15****, or L16**** or L18**** (where the asterisk could be anything) it pops up the alert box with a message saying "Yes eligible" but any other code returns "No sorry not eligible" - eg L19**** it returns nope.

I've tried hacking the code that validates to format and can manage it for just the first character - ie the L but I'm totally lost after that. I don't know if hacking the below will work.

I'm guessing I might need to add in an array of 'eligible' variables - but not sure.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function checkPostCode (toCheck) {
  2.  
  3. // Permitted letters depend upon their position in the postcode.
  4. var alpha1 = "[l]"; // Character 1
  5. var alpha2 = "[abcdefghklmnopqrstuvwxy]"; // Character 2
  6. var alpha3 = "[abcdefghjkpmnrstuvwxy]"; // Character 3
  7. var alpha4 = "[abehmnprvwxy]"; // Character 4
  8. var alpha5 = "[abdefghjlnpqrstuwxyz]"; // Character 5
  9.  
  10. // Array holds the regular expressions for the valid postcodes
  11. var pcexp = new Array ();
  12.  
  13. // Array holds the regular expressions for the valid postcodes
  14. var pcexp = new Array ();
  15.  
  16. // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
  17. pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1,2})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  18.  
  19. // Expression for postcodes: ANA NAA
  20. pcexp.push (new RegExp ("^(" + alpha1 + "{1}[0-9]{1}" + alpha3 + "{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  21.  
  22. // Expression for postcodes: AANA NAA
  23. pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "{1}" + "?[0-9]{1}" + alpha4 +"{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  24.  
  25.  
  26. // Load up the string to check
  27. var postCode = toCheck;
  28.  
  29. // Assume we're not going to find a valid postcode
  30. var valid = false;
  31.  
  32. // Check the string against the types of post codes
  33. for ( var i=0; i<pcexp.length; i++) {
  34. if (pcexp[i].test(postCode)) {
  35.  
  36.  
  37.  
  38. // Load new postcode back into the form element
  39. valid = true;
  40.  
  41. // Remember that we have found that the code is valid and break from loop
  42. break;
  43. }
  44. }
  45.  
  46. // Return with either the reformatted valid postcode or the original invalid
  47. // postcode
  48. if (valid) {return postCode;} else return false;
  49. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
davidjcolbran is offline Offline
5 posts
since Oct 2008
Jan 29th, 2010
1
Re: Trying to hack a Javascript to validate a form on just a few variables in one field
Managed to figure it out ... I think i was over complicating things looking at that validation format script.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script>
  2. function validate()
  3. {
  4. var str = document.getElementById("pc").value; // receives L16ABC
  5. var pcode = str.substr(0,3); // gets L16 only
  6. if (pcode == "L15" || pcode == "L16" || pcode == "L18")
  7. alert ("Eligible")
  8. else
  9. alert ("Not Eligible");
  10. }</script>

And the call from the form;

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <input type="text" name="pc" id="pc" />
  2. <button name="mybutton" type="button" onclick="validate();" style="margin-left: 30px; color: #f00;">Check</button>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
davidjcolbran is offline Offline
5 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: How to save the contents of Data URI to image.
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Looking for genie scroller





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC