Skip to content

Instantly share code, notes, and snippets.

@cauerego
Created March 16, 2011 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cauerego/873257 to your computer and use it in GitHub Desktop.
Save cauerego/873257 to your computer and use it in GitHub Desktop.
postcode on jquery using the validate plugin - try: http://jsbin.com/gist/873257#preview ( originally at http://jsbin.com/ahufe4/3/edit )
<!DOCTYPE html>
<html>
<head>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script class="jsbin" src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script>
<script class="jsbin" src="http://rez.googlecode.com/svn-history/r250/trunk/libs/jquery.maskedinput-1.2.2.min.js"></script>
<script src="jquery.validate.postcode.js"></scrip>
<script type="text/javascript">
$(function () {
$("#postcode").mask("?99999-999", {placeholder: " "}) // Codigo Endereco Postal - CEP from Brazil
var $postcode = $("#postcode").attr("name");
var $params = {debug:false, rules:{}, messages:{}};
// this is
$params['rules'][$postcode] = {"minlength": 9, "postcode": ["0","11","87654321"]};
$params['messages'][$postcode] = "<b>Invalid Postal Code</b>";
$("#frm").validate($params);
// $("#frm").validate($params); $("#postcode").rules("add", "postcode");
});
</script>
<title>Validate Postal Code</title>
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style>
article, aside, figure, footer, header, hgroup, menu, nav, section { display: block; }
</style>
</head>
<body>
<form action="" method="post" id="frm">
<fieldset>
<legend>Validate Postal Code</legend>
<label for="postcode">Postal Code:</label>
<input type="text" name="postcode" id="postcode" minlegnth="9" maxlength="9" />
<input type="submit" value="validar" />
</fieldset>
</form>
</body>
</html>
(function ($) {
$.validator.addMethod('postcode',function(value,element,param) {
$return = false;
value = value.replace("-","");
$(param).each(function(i){
if ( value.substr(0, this.length) == this ) $return = true;
});
return $return;
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment