我想添加一個額外的驗證要求,目前正常工作的窗體。驗證輸入形式
現在javascript檢查以確保表單在提交之前具有以下值。
必須在該字段中輸入名字。
必須在該字段中輸入姓氏。
必須在此輸入ID號碼。該值必須以「V」開頭,後面跟着「0」(零),然後是7個數字。
必須在該字段中輸入用戶名。
我關心的是#3。如果該字段的值不是以「V」開始,後面跟着「0」或「9」,然後是7個數字,我想添加一個額外的需求來產生對話框消息。
例如,這些值將工作:
V
V91234567
這是行不通的:
V0wewewew
V11234567
我試圖改變:
if(rid.charAt(0) != 'V' || rid.charAt(1) != '0'){
這個
if(rid.charAt(0) != 'V' || rid.charAt(1) != '0' || rid.charAt(1) != '9'){
但它不起作用。
我將不勝感激任何幫助。
謝謝, 邁克
<script type="text/javascript">
function checkrequired(which) {
\t var fn = document.getElementById("fn").value;
\t var ln = document.getElementById("ln").value;
\t if(fn == ''){
\t \t alert("Please enter your First Name");
\t \t return false;
\t }
\t if(ln == ''){
\t \t alert("Please enter your Last Name");
\t \t return false;
\t }
\t var rid = document.getElementById("rid").value;
\t var nb = rid.substring(2);
\t if(rid.charAt(0) != 'V' || rid.charAt(1) != '0'){
\t \t alert("Enter your ID correctly: V0 followed by seven numbers.");
\t \t return false;
\t }else if(nb.length != 7){
\t \t alert("Enter your ID correctly: V0 followed by seven numbers.");
\t \t return false;
\t }else if(isNaN(nb)){
\t \t alert("Enter your ID correctly: V0 followed by seven numbers.");
\t \t return false;
\t }
\t var un = document.getElementById("un").value;
\t if(un == "Enter Username" || un == ''){
\t \t alert("Please make sure that you entered a valid username");
\t \t return false
\t }
}
</script>
</head>
<body>
<div id="wrapper">
\t
<div id="main_content">
<FORM action="page2.html" method="POST" name=myform onSubmit="return checkrequired(this)">
<p>
\t Enter your first name, last name, and Identification Number (ID) into the spaces below, and then click the Continue button.
</p>
<p>
\t 1. First Name:
\t <input id="fn" type="text" name="requiredFirstName" value="" size="20" maxlength="20">
</p>
<p>
\t 2. Last Name:
\t <input id="ln" type="text" name="requiredLastName" value="" size="20" maxlength="20">
</p>
<p>
\t 3. Enter your Identification Number:
\t <input id="rid" type="text" name="requiredID" placeholder="V0xxxxxxx" size="20" maxlength="20">
</p>
<p>
\t 4. Enter your Username:
\t <input id="un" type="text" name="requiredUsername" placeholder="Enter Username" size="15" maxlength="50"><strong>@mail.mydomain.com</strong>
</p>
<p>
\t <input type="submit" name="Submit" value="Continue">
</p>
</FORM>
</div>
</div>
'/^V [09] \ d {7} $ /' –
爲什麼你就不能使用這個正則表達式?這是一個完美的用例。 –