所以我目前有这种强制密码的方法:
$.validator.addMethod('goodPassword', function(value, element){
return this.optional(element) ||
value.length >= 6 &&
/\d/.test(value) &&
/[a-z]/i.test(value);
}, 'Your password sucks boy, you need at least 6 char and both number and char in that motherf.')
我想为我的用户名创建类似的内容,以将其限制为字母、数字、破折号、下划线和句点。我似乎不知道该怎么做。我读了一些关于正则表达式的内容,但仍然无法弄清楚我应该如何处理我的代码。
这是我目前拥有的:
$.validator.addMethod('goodUsername', function(value, element)){
return this.optional(element) ||
value.length >= 4 // && or ||, not really sure what to put here
//something to check if entered data contains anything other than letters, numbers, dashes, underscores and periods
}, 'What kind of fkin username is that? You can only use letters, numbers, dashes, underscores and periods.')
有人可以给我指路吗?
最佳答案
这是使用 Javascript 正则表达式功能的代码
$.validator.addMethod('goodUsername', function(value, element)){
return this.optional(element) || value.match(/^[\w.-]{4,}$/m)
}, 'What kind of fkin username is that? You can only use letters, numbers, dashes, underscores and periods.')
正如 @rock321987 所正确指出的,它仍然允许 ...-...
和其他奇怪的用户名。
关于javascript - 如何为用户名约束创建 jQuery Validator 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38149227/