Monday, 15 June 2015

regex - jQuery Validate method checking for special characters -



regex - jQuery Validate method checking for special characters -

i trying (and failing) write regular look statement checks special characters such !@#$%^&*()_+<>?'"{}[] in javascript form validation.

i understand has been asked 1000 times i'm under serious time pressure. if rather not reply question below , able point me in direction of previous reply above question appreciate it.

on similar note, can tell me why next shooting error when come in lowercase 'abc', etc? i'm baffled.

jquery.validator.addmethod("specialchars", function( value, element ) { var regex = new regexp("^[a-za-z0-9]+$"); var key = string.fromcharcode(event.charcode ? event.which : event.charcode); if (!regex.test(key)) { event.preventdefault(); homecoming false; } }, "please utilize alphanumeric or alphabetic characters");

thanks help.

cheers, tim

instead of writing own custom method scratch, include additional-methods.js file , utilize alphanumeric rule.

$(document).ready(function () { $('#myform').validate({ rules: { field: { alphanumeric: true } } }); });

demo: http://jsfiddle.net/ysakx/

if don't want include additional external file, re-create default alphanumeric method out of it...

jquery.validator.addmethod("alphanumeric", function(value, element) { homecoming this.optional(element) || /^\w+$/i.test(value); }, "letters, numbers, , underscores please");

jquery regex jquery-validate

No comments:

Post a Comment