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