We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6723
    • 93 Posts
    for specific characters in the first few fields.

    I was wondering if anyone can point me in the right direction as I’m beginning to lose the will to live ;-)

    What I want to do is to use a JS to see if a website visitor is eligible for a particular service dependant on part of 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) and hit a submit button it pops up the alert box with a message saying "Yes eligible" (or displays a message on screen) but any other code returns "No sorry not eligible" - eg L19**** it returns nope.

    I’ve looked at all sorts of validation checks but my programming skills are limited and I can’t hack anything or find a similar example anywhere. I’m guessing the eligible postcodes need to go in an array but I can’t figure it out. It needs to be Javascript rather than PHP as my it can only be client-side.

    Any ideas people?

    Thanks, David
      • 6723
      • 93 Posts
      Managed to figure it out ...

      <script>
      function validate()
      {
      var str = document.getElementById("pc").value; // receives L16ABC
      var pcode = str.substr(0,3); // gets L16 only
      if (pcode == "L15" || pcode == "L16" || pcode == "L18")
      alert ("Eligible")
      else
      alert ("Not Eligible");
      }</script>


      and the call from the form is;

      <input type="text" name="pc" id="pc" />
      <button name="mybutton" type="button" onclick="validate();" style="margin-left: 30px; color: #f00;">Check</button>