我写了这个javascript和html代码来检查用户名和密码的有效性。由于某种原因,密码的正则表达式被igonred。
function checkDetails() {
var uname = document.getElementById("uname").value;
var pass = document.getElementById("pass").value;
var reuname = /^[a-zA-Z]\w*$/;
var repass = /^\w+{4,8}$/;
if (reuname.test(uname)) alert(uname);
else {
alert("username should be alphanumeric and start with alphabet");
return false;
}
if (repass.test(pass)) alert(pass);
else {
alert("password should be alphanumeric with 4-8 chars");
return false;
}
}

body {
background-color: cyan;
}
#content {
width: 550px;
margin-left: auto;
margin-right: auto;
}
h1 {
text-align: center;
margin-bottom: 30px;
}
#content table td {
height: 30px;
}

<div id="content">
<h1>Register Form</h1>
<form method="post" action="" onsubmit="return checkDetails(); return false;">
<table>
<tr>
<td>Enter UserName:</td>
<td>
<input type="text" name="uname" size="20" id="uname" />
</td>
</tr>
<tr>
<td>Enter Password:</td>
<td>
<input type="password" name="pass" size="20" id="pass" />
</td>
</tr>
<tr>
<td>Re-Enter Password:</td>
<td>
<input type="password" name="repass" size="20" id="repass" />
</td>
</tr>
<tr>
<td>Enter E-mail:</td>
<td>
<input type="text" name="email" id="email" />
<td>
</tr>
<tr>
<td>Re-Enter E-mail:</td>
<td>
<input type="text" name="reemail" id="reemail" />
<td>
</tr>
<tr>
<td>Enter Address:</td>
<td>
<textarea rows="10" cols="50" name="address" id="address"></textarea>
<td>
</tr>
<tr>
<td colspan="2" style="text-align: center;">
<input type="submit" name="submit" value="submit" style="width: 100px; height: 30px;" />
</td>
</tr>
</table>
</form>
</div>
&#13;
当我评论这条特定的行时,它会起作用。
/*var repass = /^\w+{4,8}$/;*/
为什么这种奇怪的行为?
答案 0 :(得分:2)
检查你的控制台!你应该看到:Uncaught SyntaxError: Invalid regular expression: /^\w+{4,8}$/: Nothing to repeat(…)
我不确定你想要什么,但也许可以试试/^(\w+){4,8}$/
答案 1 :(得分:2)
function checkDetails() {
var uname = document.getElementById("uname").value;
var pass = document.getElementById("pass").value;
var reuname = /^[a-zA-Z]\w*$/;
var repass = /^\w{4,8}$/;
if(reuname.test(uname))alert(uname);
else {
alert("username should be alphanumeric and start with alphabet");
return false;
}
if(repass.test(pass))alert(pass);
else {
alert("password should be alphanumeric with 4-8 chars");
return false;
}
return true;
}
&#13;
body{
background-color: cyan;
}
#content{
width: 550px;
margin-left: auto;
margin-right: auto;
}
h1 {
text-align: center;
margin-bottom: 30px;
}
#content table td{
height: 30px;
}
&#13;
<div id="content">
<h1>Register Form</h1>
<form method="post" action="" onsubmit="return checkDetails(); return false;">
<table>
<tr>
<td>Enter UserName:</td>
<td>
<input type="text" name="uname" size="20"
id="uname" />
</td>
</tr>
<tr>
<td>Enter Password:</td>
<td>
<input type="password" name="pass" size="20"
id="pass" />
</td>
</tr>
<tr>
<td>Re-Enter Password:</td>
<td>
<input type="password" name="repass" size="20"
id="repass" />
</td>
</tr>
<tr>
<td>Enter E-mail:</td>
<td>
<input type="text" name="email" id="email"
/>
<td>
</tr>
<tr>
<td>Re-Enter E-mail:</td>
<td>
<input type="text" name="reemail" id="reemail"
/>
<td>
</tr>
<tr>
<td>Enter Address:</td>
<td>
<textarea rows="10" cols="50" name="address"
id="address"></textarea>
<td>
</tr>
<tr>
<td colspan="2" style="text-align: center;">
<input type="submit" name="submit" value="submit"
style="width: 100px; height: 30px;"
/>
</td>
</tr>
</table>
</form>
</div>
&#13;
我认为密码的正则表达式无效。你能试试吗?
答案 2 :(得分:1)
您必须删除正则表达式的“+”符号。
var repass = /^\w{4,8}$/;