【问题标题】:javascript validation issue for password密码的javascript验证问题
【发布时间】:2015-11-07 22:24:03
【问题描述】:

我有一些关于密码的 javascript 代码的问题。这是我的代码。那部分工作正常,但我想提出另一个条件,即我的密码应包含至少 8 个字符并且必须遵守以下规则:没有空格,至少包含 1 个大写字母和一个数字。关于手机,它必须始终以数字开头。 5.帮助

    function formValidation() {
 var mobile = document.forms["form"]["mobile"].value;
        var password = document.forms["form"]["password"].value;
       
//reg expression check
 var checkNumbers = /^[0-9 ]+$/;


$(document.forms["form"]["mobile"]).focus(function(){
		$(document.forms["form"]["mobile"]).css("background-color", "white");
	});
$(document.forms["form"]["password"]).focus(function(){
		$(document.forms["form"]["password"]).css("background-color", "white");
function clear(){
$(document.forms["form"]["mobile"]).focus(function(){
		$(document.forms["form"]["mobile"]).css("background-color", "white");
	});
$(document.forms["form"]["password"]).focus(function(){
		$(document.forms["form"]["password"]).css("background-color", "white");
	});
}

 if (mobile == null || mobile == "") {
        error[error.length]=("Enter your mobile number"); 
        document.form.mobile.focus();
        $(document.forms["form"]["mobile"]).css("background-color", "blue");  
        ;         
    }else if (mobile != null || mobile != "") {
    	if(!checkNumbers.test(mobile)){
    	error[error.length]=("Enter Only numeric Characters for mobile phone");
		document.form.mobile.focus();
		$(document.forms["form"]["mobile"]).css("background-color", "blue");
    	}
               
    }
        if (password == null || password == "") {
            error[error.length]=("Enter a password"); 
            document.form.password.focus();
            $(document.forms["form"]["password"]).css("background-color", "blue");   
        }

    }
   		
    <form name="form" onsubmit="return formValidation()" action="process.html">
    
Mobile phone:
		<input type="text" name="mobile" id="mobile"></br></br>

Password:
    <input type="password" name="password" id="password"></br></br>
    </form> 
<input id="submit" type="submit" name="submit" id="submit">

【问题讨论】:

    标签: javascript forms validation


    【解决方案1】:

    如果你把它分解成几个部分,你可以很容易地做到这一点,并准确地告诉用户他们失败的约束,就像这样:

    // Check the length:
    if (password.length < 8) { // ... not long enough }
    
    // Check if it has at least one upper case:
    if (password.match(/[A-Z]+/g) === null) { // ... no upper case characters }
    
    // Check if it has at least one number:
    if (password.match(/\d/g) === null) { // ... no numbers }
    
    // Password passes validation!
    

    【讨论】:

    • 嗯,我已经在这个 jsfiddle (jsfiddle.net/4w6x0311) 上工作了,你能在那里试试你的输入吗?
    【解决方案2】:

    测试是否满足所有条件(c1 条件 1 等)

    var c1 = (password.toLowerCase() != password);// if it HAS uppercase letters, it won't match
    var c2 = password.length > 8;
    var c3 = !(password.indexOf(" ") > -1); // no spaces
    var c4 = password.match(/\d+/g); // matches numbers
    

    【讨论】:

      【解决方案3】:

      您可以创建一个RegExp 对象,然后将您的密码传递给它的test 方法。 RegExp 对象可以使用正向预测来断言它找到了一个大写字符和一个数字字符。

      最后,您可以通过尝试模式匹配字符串开头标记 (^) 和结尾之间的至少 8 个非空白字符来测试密码是否至少有 8 个字符长并且不包含空白字符字符串标记 ($)。模式匹配将消耗整个字符串,因此如果找到任何空白字符,则测试将失败,如果少于 8 个字符,则测试也将失败。

      /(?=.*[A-Z])(?=.*\d)^\S{8,}$/.test(password)
       ^uppercase ^digit  ^8+ characters in length and no whitespace
      

      如果密码正确,则此表达式将评估为 true,否则将评估为 false。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-06-22
        • 2011-08-14
        • 2013-12-09
        • 2013-04-14
        • 1970-01-01
        • 2015-11-23
        • 2011-11-29
        相关资源
        最近更新 更多