Javascript 密码验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16140379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript password validation
提问by Steve Aylward
Can anyone suggest an easy password validation using javascript. I need it to be at least 4 characters long and contain min 1 letter and 1 number.
任何人都可以建议使用 javascript 进行简单的密码验证。我需要它至少有 4 个字符长,并且至少包含 1 个字母和 1 个数字。
This is a school project so its really only a simple validation needed to make sure they are entering in the required characters.
这是一个学校项目,因此它实际上只是一个简单的验证,以确保他们输入所需的字符。
I am using
我在用
<textarea id="special_info" name="address" onfocus="this.value=''" rows="5" cols="40" >Please enter your address...
</textarea>
<div id="maxcar">(Maximum characters: 200)
</div>
My javascript is in this format
我的 javascript 是这种格式
function validate_form(){
valid=true;
var letters=/^[a-zA-Z]+$/;
var numbers=/^[0-9]+$/;
var email=document.order_form.user_email.value;
var invalid = [];
if (document.order_form.first_name.value.search(letters)==-1){
invalid.push("*First Name")
}
if (document.order_form.surname.value.search(letters)==-1){
invalid.push("*Surname Name")
}
if (email.indexOf("@")<1 || email.lastIndexOf(".")<email.indexOf("@")+2
|| email.lastIndexOf(".")+2>=email.length){
invalid.push("*Email")
}
if (invalid.length != 0)
{alert("Please provide response: \n" + invalid.join("\n") );
valid = false;
invalid = [];}
return valid;}
回答by RobG
You can use a regular expression and check the length. The regular expression is a case insensitive check for one letter and one number in any order. The length must be greater than 3.
您可以使用正则表达式并检查长度。正则表达式是对一个字母和一个数字以任何顺序进行的不区分大小写的检查。长度必须大于 3。
function testString(s) {
var re = /[a-z]\d|\d[a-z]/i;
return re.test(s) && s.length > 3;
}
回答by allonhadaya
Try this:
试试这个:
// returns true if the form was valid; false otherwise.
function validateForm() {
var allLetters = /^[a-zA-Z]+$/;
var letter = /[a-zA-Z]/;
var number = /[0-9]/;
var firstName = document.order_form.first_name.value;
var surname = document.order_form.surname.value;
var email = document.order_form.user_email.value;
var password = document.order_form.password.value;
var invalid = [];
if (!allLetters.test(firstName)) {
invalid.push("*First Name");
}
if (!allLetters.test(surname)) {
invalid.push("*Surname Name");
}
if (email.indexOf("@") < 1 || email.lastIndexOf(".") < email.indexOf("@") + 2 || email.lastIndexOf(".") + 2 >= email.length) {
invalid.push("*Email");
}
if (password.length < 4 || !letter.test(password) || !number.test(password)) {
invalid.push("*Password");
}
if (invalid.length != 0) {
alert("Please provide response: \n" + invalid.join("\n"));
return false;
}
return true;
}
Give it a good read, run it, and ask questions if something is unclear.
好好阅读,运行它,如果有不清楚的地方,请提出问题。
Notice the use of RegExp.testinstead of String.search.
注意使用RegExp.test而不是 String.search。