Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用正则表达式的jQuery验证程序和可选字段_Jquery_Jquery Validate - Fatal编程技术网

使用正则表达式的jQuery验证程序和可选字段

使用正则表达式的jQuery验证程序和可选字段,jquery,jquery-validate,Jquery,Jquery Validate,我有一个可选字段(比如“text1”),它可以是空的,也可以是字母数字: jQuery.validator.addMethod("onlyAlphaNumeric", function(value, element) { var regExp = new RegExp(/^[a-zA-Z0-9]+$/); return ((this.optional(element)) || regExp.test

我有一个可选字段(比如“text1”),它可以是空的,也可以是字母数字:

jQuery.validator.addMethod("onlyAlphaNumeric", 
        function(value, element) {  
            var regExp = new RegExp(/^[a-zA-Z0-9]+$/);              
            return ((this.optional(element)) || regExp.test(value));
        }
    , "Only aplaha-numeric characters allowed");


$("#student-search-form").validate({
    rules : {
        text1 : {
            optional : true,
            onlyAlphaNumeric: "Only a-n allowed"
        }
    },
    messages: {
        text : {                    
            acceptOnly: " Only alpha-numeric characters allowed"
        }               
    }
});
问题是没有发生验证,因此如果用户在“text1”中输入“!&^%(*”,表单将被提交,没有错误检查

谁能告诉我我做错了什么? 谢谢。

这是错误的

rules : {
    text1 : {
        optional : true,
        onlyAlphaNumeric: "Only a-n allowed"
    }
},
对于
onlyaphanumeric:
,您只能将
设置为true
设置为false

我不太确定
可选:
但我知道
必需:
是有效的…所以将
必需:
设置为
false
。或者,您可以完全忽略它,因为验证器默认字段为“可选”(
必需:false
),除非您另行指定

rules : {
    text1 : {
        required : false,
        onlyAlphaNumeric: true
    }
},
看看这个类似的答案


您可以在验证方法中添加可选项-

验证-

rules : {
     text1 : {
         alphanumeric: true
     }
 },

我认为您的代码必须是这样的:

// Suppose that your method is well defined
jQuery.validator.addMethod("onlyAlphaNumeric", 
    function(value, element) {  
        var regExp = new RegExp(/^[a-zA-Z0-9]+$/);              
        return ((this.optional(element)) || regExp.test(value));
    }
, "Only alpha-numeric characters allowed");

$("#student-search-form").validate({
    rules : {
        text1 : {
        required : false, // optional must be replaced by required
        onlyAlphaNumeric: true // rules are boolean
        }
    },
    messages: {
        text1 : {
            onlyAlphaNumeric: "I can change my message : Only alpha-numeric characters allowed"
        }
    }
});

我不知道
optional
是一个已知的验证器(尽管我可能错了)。是的\w还包括一些其他验证器。这是一个使用jquery进行可选验证的示例。需要根据需要进行定制。还有意思的是,作者使用I标志表示与
[0-9a-zA-Z_2;]同义的模式
是的..正在检查相同的内容。\w应该包含所有大小写字符。需要检查是否包含任何特定原因。
必需的默认值为false,因此无需声明它(除非是为了清晰起见)。而且,在这一切都说了又做了之后……@BradChristie,我想是的,我已经说过类似的话了。“或者,你可以完全忽略它,因为验证器默认字段为“可选”,除非你另有说明。”谢谢,有几件事是错的(如前所述),required:false而不是optional:true(或者根本不需要required:false,因为这是默认设置),正如@Sparky672所提到的,“onlylphanumeric:,您只能设置true或false。”
// Suppose that your method is well defined
jQuery.validator.addMethod("onlyAlphaNumeric", 
    function(value, element) {  
        var regExp = new RegExp(/^[a-zA-Z0-9]+$/);              
        return ((this.optional(element)) || regExp.test(value));
    }
, "Only alpha-numeric characters allowed");

$("#student-search-form").validate({
    rules : {
        text1 : {
        required : false, // optional must be replaced by required
        onlyAlphaNumeric: true // rules are boolean
        }
    },
    messages: {
        text1 : {
            onlyAlphaNumeric: "I can change my message : Only alpha-numeric characters allowed"
        }
    }
});