Search code examples
phpregexjoomlavirtuemart

zip/Postcode Digit valdiation check


I need to set the postcode field to be validated to have at least one number in it but seems like im missing something becuase it is not working:

 if( isset( $required_fields['zip'] )) {
         echo '
         if( =(/^(?=.*[0-9])(?=.*[a-zA-Z])+$/.test(form.zip.value))) {
            alert( \'Please enter a valid Zip Code\');
            return false;
         }';

      }

can someone tell me what is wrong please?

Thanks in advance


Solution

  • The pattern you need is this: ^(?=.*[0-9])([\w]+)$

    You were using positive look-ahead on [a-zA-Z] instead of actual character matching, and the look-aheads match only a position, not characters. Of course, the pattern [\w] can be replaced with some more restrictive pattern.