Validate zip and display error with onBlur event

Posted by phil on Stack Overflow See other posts from Stack Overflow or by phil
Published on 2010-06-11T07:16:47Z Indexed on 2010/06/11 7:22 UTC
Read the original article Hit count: 234

Filed under:

Check if zip is 5 digit number, if not then display 'zip is invalid'. I want to use onBlur event to trigger the display. But it's not working.

<script>
$(function(){

function valid_zip()
  {
  var pat=/^[0-9]{5}$/;
  if ( !pat.test(   $('#zip').val()   ) )
     {$('#zip').after('<p>zip is invalid</p>');}
  }

})
</script>

zip (US only) <input type="text" name='zip' id='zip' maxlength="5" onBlur="valid_zip()">

© Stack Overflow or respective owner

Related posts about jQuery