为密码验证JavaScript添加条件

时间:2014-06-05 08:56:11

标签: javascript

我需要为此密码添加一些进一步的验证。我创建了一个if语句,如果任何条件失败,失败的条件将打印出相关的消息。我尝试添加验证:

  • 密码必须为1位数(0-9)
  • 密码必须有1个UpperCase(A-Z)
  • 密码必须有1个LowerCase(a-z)

Javascript代码:

<script type="text/javascript">
function check_form() {
    var passw = document.getElementById('password-input-0').value;
    var passw2 = document.getElementById('password-input-1').value;
    var letter = /[a-zA-Z]/;
    var number = /[0-9]/;

    if (passw.length < 6 || passw != passw2 || !letter.test(passw) || !number.test(passw)) {
        if (passw.length < 6) {
            alert("Please make sure password is longer than 6 characters.")
            return false;
        }
        if (passw != passw2) {
            alert("Please make sure passwords match.")
            return false;
        }
        if (!letter.test(passw)) {
            alert("Please make sure Password Includes an UpperCase and LowerCase character")
            return false;
        }
        if (!number.test(passw)) {
            alert("Please make sure Password Includes a Digit")
            return false;
        }

        /*email test*/
        var email = document.getElementById('email-input-0').value;
        var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if (!filter.test(email)) {
            alert('Please provide a valid email address');
            form.email.focus;
            return false;
        }


        return true;
    }
}
</script>

这段代码似乎不起作用,并且是一个缺乏经验的JS程序员,我不确定它为什么不起作用。我已离开电子邮件验证,以确保修复程序不会干扰它。谢谢你的阅读!

Fiddle

2 个答案:

答案 0 :(得分:3)

您的代码中存在一些问题。首先,您错过了几个}个大括号 - 例如,您的第一个主if子句的结尾。

其次,字符检查有点瑕疵 - 它在同一个检查中检查大写和小写,因此不应该有效的密码通过。

我已经更新了小提琴,它现在包含正确的代码,解决了这两个问题:http://jsfiddle.net/3kPkQ/3/

function check_form()
{
var passw = document.getElementById('password-input-0').value;
var passw2 = document.getElementById('password-input-1').value;
var letter = /[a-z]/;
var upper  =/[A-Z]/;
var number = /[0-9]/;

if(passw.length < 6 || passw != passw2 || !letter.test(passw) || !number.test(passw) || !upper.test(passw)) {
  if(passw.length<6){
    alert("Please make sure password is longer than 6 characters.")
    return false;
  }
  if(passw != passw2){
    alert("Please make sure passwords match.")
    return false;
  }
  if(!letter.test(passw)){
    alert("Please make sure password includes a lowercase letter.")
    return false;
  }
  if(!number.test(passw)){
    alert("Please make sure Password Includes a Digit")
    return false;     
  }
  if(!upper.test(passw)) {
    alert("Please make sure password includes an uppercase letter.");
    return false;
  }
}

/*email test*/
var email = document.getElementById('email-input-0').value;
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (!filter.test(email)) {
  alert('Please provide a valid email address');
  form.email.focus;
  return false;
}


return true;
}

这应该有效 - 修复了@faby的答案中的第二个问题。

答案 1 :(得分:0)

工作小提琴

http://jsfiddle.net/3kPkQ/5/

   function check_form()
{
var passw = document.getElementById('password-input-0').value;
var passw2 = document.getElementById('password-input-1').value;
var letter = /[a-z]/;
 var letterUp= /[A-Z]/;
var number = /[0-9]/;

if(passw.length < 6 || passw != passw2 || !letter.test(passw) || !number.test(passw) ) {
      if(passw.length<6){
      alert("Please make sure password is longer than 6 characters.")
      return false;
      }
      if(passw != passw2){
      alert("Please make sure passwords match.")
      return false;
      }
      if(!letter.test(passw) || !letterUp.test(passw)){
      alert("Please make sure Password Includes an UpperCase and LowerCase character")
      return false;
      }
      if(!number.test(passw)){
      alert("Please make sure Password Includes a Digit")
      return false;     
  }

  /*email test*/
var email = document.getElementById('email-input-0').value;
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  if (!filter.test(email)) {
     alert('Please provide a valid email address');
     form.email.focus;
     return false;
}


return true;
}
}

HTML

<input type="text" id="password-input-0"/>
<input type="text" id="password-input-1"/>
<input type="button" value="clickme" id="password-input-2" onclick="check_form()"/>