gpt4 book ai didi

Javascript防止点逗号等

转载 作者:行者123 更新时间:2023-12-02 19:10:55 24 4
gpt4 key购买 nike

我有 javascript 验证代码,现在我想阻止 。 ; ,`:

这是我到目前为止的代码:

jQuery.validator.addMethod("noSpace", function(value, element) {
return !value.match(/\s/g);
});

$().ready(function() {
$("#signup_form").validate( {
rules: {
full_name: "required",
username: {
required: true,
minlength: 2,
noSpace: true,
},
password: {
required: true,
minlength: 5
},
email: {
required: true,
email: true
},
},
messages: {
full_name: "<div class='spacing'>Please enter your full name</div>",
username:
{
required: "<div class='spacing'>Please enter an username</div>",
minlength: "<div class='spacing'>Your username must consist of at least 2 characters</div>",
noSpace: "<div class='spacing'>No space allowed</div>",
},
password:
{
required: "<div class='spacing'>Please provide a password</div>",
minlength: "<div class='spacing'>Your password must be at least 5 characters long</div>"
},
email: "<div class='spacing'>Please enter a valid email address</div>"
}
});
});

我想在用户名规则中阻止。所以如果我输入 . , ; 等,它会显示不允许的消息。

最佳答案

未经测试,但这应该有效:

jQuery.validator.addMethod("userNameRule", function(string, element) { 
return !string.match(/[.;,]/g);
}, "Your username contains invalid characters!");

username: {
required: true,
minlength: 2,
noSpace: true,
userNameRule: true
},

关于Javascript防止点逗号等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13738710/

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