0

The data entered should be processed to check if the zip code is included in service area (This includes zip codes from 63121 to 69999).

This is what I have done I know I'm doing pattern matching wrong. Any help will be appreciated!

function validate() {
    var zipCode = document.getElementById("zip");
    var ok = zipCode.value.search(/^(6|9)\d{3,9}\d{1,9}\d{2,9}\d{1,9}$/);
    if (ok != 0) {
        alert("Sorry no service");
        zipCode.focus();
        zipCode.select();
        return false;
    } else
        return true;
}

HTML Part

<form name = "myForm" action = ""  >

        <table align = "center" bgcolor = "FF6600" cellspacing = "3" cellpadding = "4" border = "1" style="font-family:Comic Sans;vertical-align:center;">
        <caption style="background-color:#FFD700; text-align:left"> <font size="5" color="black">Check Coverage: </font></caption>
            <tr>
                <td class="info">First Name</td>
                <td><input type = "text" name = "first" class="textright">*</td>
            </tr>
            <tr>
                <td class="info">Phone Number</td>
                <td><input type  = "text"  name  = "phone" size="10" maxlength = "12" class="textright" >* (ddd-ddd-dddd)</td>
            </tr>
            <tr>
                <td class="info">Zip Code</td>
                <td><input type  = "text" id="zip" name = "zip1" size ="5" maxlength = "5" class="textright">* </td>
            </tr>
            <tr>
                <td class="size">* - Required Field</td>
                <td><input type = "button" class="but2" value ="Reset"><input type = "submit" class= "but2" onclick="validate()" value ="Check Availability" ></td>
            </tr>
        </table>
</form>
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
sean
  • 77
  • 1
  • 1
  • 6
  • Is this a duplicate of - http://stackoverflow.com/questions/578406/what-is-the-ultimate-postal-code-and-zip-regex ?? – Leptonator Oct 30 '13 at 04:35
  • @Leptonator Not really a dupe, as this is asking about a very specific range of postcodes. –  Oct 30 '13 at 04:36
  • I've update the answer to meet your updated question. However as @MikeEdwards suggested: in future, you might want to either ask two questions, or scope the initial question better. –  Oct 30 '13 at 05:52

2 Answers2

2

There is no need to use regex. Since all of the zipcodes you are looking at are all 5 digits long (with no leading zeroes), cast them as an integer and just check the zipcode is in the required range.

function validate() {
    var zipCodeField = document.getElementById("zip");
    var zipCode = parseInt(zipCodeField.value);
    if (!(63121 <= zipCode && zipCode <= 69999)) {
        alert("Sorry no service");
        zipCodeField.focus();
        zipCodeField.select();
        return false;
    } else {
        return true;
    }
}

With the update about it not working, I threw together this fiddle. The only thing you need to do is return the value of the validate() function, onsubmit of the form rather than onclick of the button.

  • @sean Well, there is no code that shows how this function is being called, so I don't know why that might be. But this should work as requested. –  Oct 30 '13 at 04:53
  • 1
    If you need help getting event handlers to work, that's really a different question. Lego Stormtroopr answered the question you asked, you should mark it as answered and open a new question if necessary. – Mike Edwards Oct 30 '13 at 05:00
-1

I saw that your requirement is linear..

var x=parseInt(zipcode)
if(zipcode<63121 and zipcode>69999){
alert("Sorry no service");
        zipCode.focus();
        zipCode.select();
        return false;
} 
else 
{
return true;
}
sam
  • 2,426
  • 2
  • 20
  • 29