1
$('#create_membership').click(function () {
    var zip = $('#zip').val();

    else if (zip == ''){
        errorMessage = "*Zipcode required!";
    }
    else if ((zip.length)< 5 || (zip.length)>5 ){
        errorMessage = "*zipcode should only be 5 digits";
    }
    else if ( zip =( "^[0-9]+$" )){
        errorMessage = "*zipcode should be numbers only";
    }

I am getting first and second i.e., empty and length validations for zip code.but for the 3rd case,number validation is not working.can any one help me for getting the number validation thanks in advance

Musa
  • 96,336
  • 17
  • 118
  • 137
user1334095
  • 87
  • 2
  • 4
  • 13
  • 1
    1. look into jQuery validation plugin, writing all this yourself is wildly unnecessary, 2. look at this page to see how regex in javascript works: https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions (hint: you're currently simply checking to see if zip is equal to the literal string "^[0-9]+$" not evaluating it against that regular expression). You could also simply do parseInt(zip, 10) when you read it in and your length check would take care of it if it's not a valid number. – Paolo Bergantino Jul 13 '12 at 14:41
  • you are using regular expressions. you have to use zip.match(/^[0-9]+$/) into your ifelse function – user1269989 Jul 13 '12 at 14:46

4 Answers4

13

This isn't really jQuery-specific, but you have several syntax errors and your validation logic can be greatly simplified:

$('#create_membership').click(function()
{
    var zip = $('#zip').val();

    var zipRegex = /^\d{5}$/;

    if (!zipRegex.test(zip))
    {
        // trigger error
    }
    else
    {
        // success!
    }
});

I should note that validating a ZIP code isn't necessarily just checking for 5 numbers. I'd also try and find a listing somewhere and make sure what they entered is a valid US ZIP code. Additionally, you might also want to allow for the 4-digit supplemental code as well.

Jimmy Sawczuk
  • 13,488
  • 7
  • 46
  • 60
  • If you want to check for 5 digit US zip codes with the optional 4 extra digits, you could do this: `/^\d{5}(-\d{4)?$/` – Jim Rubenstein Aug 02 '12 at 20:05
  • 2
    The above regex has a typo (for some reason I can't edit it). The correct format should include a `}` after the `4` like this -- `/^\d{5}(-\d{4})?$/` – Stephen Sprinkle Dec 17 '12 at 20:05
3

You need to treat the regular expression like a function, otherwise you're just assigning the value to "zip". See below:

$('#create_membership').click(function () {
  var zip = $('#zip').val();
  var reg = /^[0-9]+$/;
  else if (zip == ''){
  errorMessage = "*Zipcode required!";
  }
  else if ((zip.length)< 5 || (zip.length)>5 ){
  errorMessage = "*zipcode should only be 5 digits";
  }
  else if (!reg.test(zip)){
  errorMessage = "*zipcode should be numbers only";
  }
});
Developer
  • 2,021
  • 12
  • 11
0

You can try something like this - How to allow only numeric (0-9) in HTML inputbox using jQuery?

If you want just the regexp, try this - jquery: validate that text field is numeric

Community
  • 1
  • 1
Pirozek
  • 1,250
  • 4
  • 16
  • 25
0

number validation

var data=$('#zip').val();
var len=data.length;
var c=0;
for(var i=0;i<len;i++)
{
  c=data.charAt(i).charCodeAt(0);
  if(c <48 || c >57)
  {
    $('.error').show();
    event.preventDefault();
    break;
  }
  else
  {
    $('.error').hide();
  }
}
swaroop suthar
  • 632
  • 3
  • 19