Skip to main content
added 32 characters in body
Source Link

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'); } 

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?

if( value.length<5 || value==$(this).attr('id') ) { $(this).addClass('error'); error++; } else { $(this).addClass('valid'); } 

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'); } 
edited tags
Link
ddavison
  • 29.3k
  • 17
  • 90
  • 116
Source Link

Using REGEX inside an IF function

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?

if( value.length<5 || value==$(this).attr('id') ) { $(this).addClass('error'); error++; } else { $(this).addClass('valid'); }