gpt4 book ai didi

jquery - 将正则表达式添加到 jquery.validate

转载 作者:行者123 更新时间:2023-12-03 22:12:06 25 4
gpt4 key购买 nike

需要向我的 jquery.validate 脚本添加正则表达式检查。

我有这个:

$().ready(function() {
$("#myBlahForm").validate({

rules: {
someName: {
required: true,
minlength: 5,
maxlength: 8,
},
somePassword: {
required: true,
minlength: 5,
maxlength: 8,
},
someConfirmPassword: {
required: true,
equalTo: "#somePassword"
},
},
messages: {
someName: {
required: "Please create your username",
},
somePassword: {
required: "Please create a password",
},
someConfirmPassword: {
required: "Please re-enter your password",
equalTo: "Passwords must match"
},
}
});

});

当用户在此处输入数据时,我想检查数据库中允许使用密码的特定字符:

    <label for="someName">Username</label>
<input type="text" id="someName" name="someName" placeholder="Create a Username"/>

<label for="somePassword">Password</label>
<input type="password" id="somePassword" name="somePassword" placeholder="Create a Password"/>

<label for="someConfirmPassword">Confirm Password </label>
<input type="password" id="someConfirmPassword" name="someConfirmPassword" placeholder="Verify your Password"/>

使用 bassisstance 的 jquery.validate 插件将正则表达式检查添加到必填字段(例如密码或用户名)的最佳方法是什么:http://docs.jquery.com/Plugins/Validation

基本上,我需要确保他们创建的密码和用户名仅包含 0-9、字母和几个特殊字符。

最佳答案

好吧,您可以使用 addMethod 添加自定义验证方法,比如

$.validator.addMethod("regx", function(value, element, regexpr) {          
return regexpr.test(value);
}, "Please enter a valid pasword.");

并且,

...
$("#myBlahForm").validate({

rules: {
somePassword: {
required: true ,
//change regexp to suit your needs
regx: /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])\w{6,}$/,
minlength: 5,
maxlength: 8
}
}

关于jquery - 将正则表达式添加到 jquery.validate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13785529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com