jQuery Validate method checking for special characters

后端 未结 3 1974
悲哀的现实
悲哀的现实 2020-12-15 12:59

I am trying (and failing) to write a regular expression statement that checks for special characters such as !@#$%^&*()_+<>?\'\"{}[] in my Javascript form validation.

3条回答
  •  余生分开走
    2020-12-15 13:34

    Instead of writing your own custom method from scratch, include the additional-methods.js file and use the alphanumeric rule.

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

    Demo: http://jsfiddle.net/YsAKx/


    If you don't want to include an additional external file, simply copy the default alphanumeric method out of it...

    jQuery.validator.addMethod("alphanumeric", function(value, element) {
        return this.optional(element) || /^\w+$/i.test(value);
    }, "Letters, numbers, and underscores only please");
    

提交回复
热议问题