我寫了這個javascript和html代碼來檢查用戶名和密碼的有效性。由於某些原因,密碼的正則表達式被忽略。
忽略密碼的正則表達式
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>
當我評論這一行,但是它的工作原理。
/*var repass = /^\w+{4,8}$/;*/
爲什麼這種奇怪的行爲?
你是什麼意思它被「忽略」? –
執行過程中未檢查'uname' – Pradeep
您檢查了控制檯嗎?您的正則表達式中有語法錯誤。 –