2016-05-04 128 views
-2

我寫了這個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}$/;*/ 

爲什麼這種奇怪的行爲?

+0

你是什麼意思它被「忽略」? –

+0

執行過程中未檢查'uname' – Pradeep

+1

您檢查了控制檯嗎?您的正則表達式中有語法錯誤。 –

回答

2

檢查您的控制檯!你應該看到有:Uncaught SyntaxError: Invalid regular expression: /^\w+{4,8}$/: Nothing to repeat(…)

我不知道你想要的究竟是什麼,但也許試試這個,而不是/^(\w+){4,8}$/

+0

你知道你的正則表達式匹配嗎?提示:這沒有意義。 –

+0

它可能不是XD我不知道他想要它匹配什麼。主要觀點是語法不正確 –

+0

某人如何高舉這個答案? –

1

你必須刪除您的正則表達式的「+」號。

var repass = /^\w{4,8}$/; 
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; 
 
}
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>

我覺得對於密碼的正則表達式無效。你可以試試這個。