Skip to content

Instantly share code, notes, and snippets.

@dshaw002
Created August 14, 2012 20:09
Show Gist options
  • Save dshaw002/3352397 to your computer and use it in GitHub Desktop.
Save dshaw002/3352397 to your computer and use it in GitHub Desktop.
Valdiator
$(document).ready(function(){
jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
phone_number = phone_number.replace(/\s+/g, "");
/*return this.optional(element) || phone_number.length > 9 &&
phone_number.match(/^\(\d{3}\) ?\d{3}( |-)?\d{4}|^\d{3}( |-)?\d{3}( |-)?\d{4}/);*/
return this.optional(element) || phone_number.length > 9 &&
phone_number.match(/^(\d{3})(-\d{3})(-\d{4})$/);
}, "Please specify a valid phone number<br>Example: 204-303-3403");
jQuery.validator.addMethod("zip", function(zip, element) {
zip = zip.replace(/\s+/g, "");
return this.optional(element) || zip.length > 4 &&
zip.match(/^\d{5}([\-]\d{4})?$/);}, "Please enter a valid zip code");
$("#frm_edit_contact").validate({
rules: {
field: {
required: true,
phoneUS: true,
zip: true
}
}
});
});
<div class="today1_middfilled" style="width:300px;">
<b>Phone</b><br>
<input type="text" class="today1_middinp phoneUS" value="<?=$phone; ?>" name="phone" id="phone"
style="width:250px;" maxlength="15"/>
<div style="color:#FF0000;" id="err_phone_name"><?=$err_phone_name; ?></div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment