简体   繁体   English

密码正则表达式验证?

[英]Password Regular expression validation?

I got 2 password fields called password and confirm password. 我有2个密码字段,称为密码,并确认密码。 I managed to validate it to make sure that the passwords must match. 我设法对其进行验证,以确保密码必须匹配。 However, I do not know how to add regular expressions to the password field to allow it 6 characters long, containing at least an uppercase and a number. 但是,我不知道如何在密码字段中添加正则表达式以使其长度为6个字符,并至少包含一个大写字母和一个数字。

This is the javascript i got so far 这是我到目前为止获得的JavaScript

<script type="text/javascript" language="javascript">

function validatepw() {
  if ( document.register.user_password.value != document.register.user_password_confirm.value)
    {
    alert('Passwords did not match!');
        return false;
        }else{
        document.register.submit();
        return true;
    }
    }
</script>

and this is my form 这是我的表格

<form name="register" action="signup.php" onsubmit="return validate_reg()" 
enctype="multipart/form-data" method="post" >

<table width="600" border="0">
<tr><td width="210" height="45">Username*:</td><td>
<input type="text" size="40" name="userUsername" id="user_username" /></td></tr>
<tr><td width="210" height="45">Password*:</td><td>
<input type="password" size="40" name="userPassword" id="user_password"/></td></tr> 
    <tr><td width="210" height="45">Re-type Password:</td><td>
<input type="password" size="40" name="userPasswordConfirm" 
id="user_password_confirm" onchange="javascript:validatepw()"/></td></tr>
</table>
<center><input type="submit" class="button" name="submit" value="register"></center>    
</form>

Can anyone show how to apply the regular expression of 6 characters, an uppercase and a number to the password field. 任何人都可以显示如何将6个字符,一个大写字母和一个数字的正则表达式应用于密码字段。 I have searched a lot and cant find anything that works with what i already got. 我已经搜索了很多东西,找不到与我已经拥有的东西相符的任何东西。

无需尝试将所有这些压缩到一个正则表达式中。

password.match(/[A-Z]/) && password.match(/[0-9]/) && (password.length >= 6)

You certainly can use a single regex for this if you'd like to: 如果您愿意,您当然可以为此使用一个正则表达式:

var isValid = /(?=.*\d)(?=.*[A-Z]).{6,}/.test(password)

(Lookahead for at least one digit, lookahead for at least one letter, then match anything, 6 or more times) (先查找至少一位数字,先查找至少一个字母,然后匹配6次或更多次)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM