0

I am trying to validate zip codes using an if function with a regex. Can this be done? I currently just have the if function making sure the zip code is 5 numbers.

below is the regex i want to use

(^\d{5}(-\d{4})?$)|(^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$) 

Can someone show me where and how i would add this to the if function below?

var value = $(this).val(); if( value.length<5 || value==$(this).attr('id') ) { $(this).addClass('error'); error++; } else { $(this).addClass('valid'); } 
5
  • to what element you want to test against Commented Jun 25, 2013 at 4:28
  • @DevZer0: Thought the same thing but there are other letters missing, F,I,O,Q,U,W,Z Commented Jun 25, 2013 at 4:28
  • @elclanrs confusing to answer this because he didn't specify which element to test it against with Commented Jun 25, 2013 at 4:30
  • it is being tested against an input value that is supposed to be a zip code. As of now the only verification is that the zip code has to be 5 numbers as you can see above Commented Jun 25, 2013 at 4:31
  • added the var value if that helps Commented Jun 25, 2013 at 4:38

2 Answers 2

1
var ZipCode = "(^\d{5}(-\d{4})?$)|(^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$)"; if (ZipCode.test(98800)) { // true } else { // false } 

Try this

Sign up to request clarification or add additional context in comments.

Comments

1

Try this

var filter = "(^\d{5}(-\d{4})?$)|(^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$)"; if (!filter.test($(this).attr('id').value)) { $(this).addClass('error'); error++; } else { $(this).addClass('valid'); } 

1 Comment

Strings don't have a test method

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.