2014-10-19 76 views
1

我想驗證輸入,到目前爲止我只創建了兩條規則。一個用來測試電話號碼,另一個用來測試輸入的密碼是否相同。javascript驗證 - Javascript沒有運行

我的問題是,由於某種原因,我的JavaScript沒有驗證輸入。我在<script>中引用它,我把它稱爲onsubmit="return validate()"。出於某種原因,即使使用警報測試來檢查其運行,也會失敗。所以,我不確定有什麼不對,我可以用一些額外的眼睛來做。

function validate() { 
 
    var errMsg = ""; /* stores the error message */ 
 
    var result = true; /* assumes no errors */ 
 
    var phonetest1 = true; 
 
    var phonetest2 = true; 
 

 
    /*get values from the form*/ 
 
    var FirstName = document.getElementById("FirstName").value; 
 
    var Lastname = document.getElementById("Lastname").value; 
 
    var Email = document.getElementById("Email").value; 
 
    var Password = document.getElementById("Password").value; 
 
    var ConPassword = document.getElementById("ConPassword").value; 
 
    var Phone = document.getElementById("Phone").value; 
 

 
    var phonepatt1 = (/\(|0|\d|\)|\d|\d|\d|\d|\d|\d|\d|\d/); 
 
    var phonepatt2 = (/0|\d|\s|\d|\d|\d|\d|\d|\d|\d|\d/); 
 

 
    /* Rule one */ 
 
    if (!phonepatt1.test(Phoneno)) { 
 
    phonetest1 = false; 
 
    } 
 
    if (!phonepatt2.test(Phoneno)) { 
 
    phonetest2 = false; 
 
    } 
 

 
    if (phonetest1 == false && phonetest2 == false) { 
 

 
    errMsg += "Your Phone number is incorrect .\n"; 
 
    result = false; 
 

 
    } 
 
    alert("I'm running"); /* This isn't working */ 
 

 
    /* Rule two */ 
 
    if (ConPassword != Password) { 
 
    errMsg += "Please confirm your password .\n"; 
 
    result = false; 
 
    } 
 

 

 
    if (errMsg != "") { //only display message box if there is something to show 
 
    alert(errMsg); 
 
    } 
 

 
    return result; 
 
}
<H1>store Home Page</H1> 
 
<p>Customer Registration: <a href="register.htm">Register</a> 
 
    <p>Customer Login: <a href="login.htm">Login</a> 
 
    <p>Manager Login <a href=" mlogin.htm">Administrators</a> 
 

 
     <form id="UserDetails" method="post" onsubmit="return validate()" action="index.htm"> 
 
     <fieldset id="Details"> 
 
      <legend>Your details:</legend> 
 
      <p> 
 
      <label for="FirstName">First Name</label> 
 
      <input type="text" name="FirstName" id="FirstName" pattern="[a-zA-Z]+" size="20" maxlength="20" required="required" /> 
 
      </p> 
 
      <p> 
 
      <label for="Lastname">Last Name</label> 
 
      <input type="text" name="LastName" id="Lastname" pattern="[a-zA-Z]+" size="20" maxlength="20" required="required" /> 
 
      </p> 
 
      <p> 
 
      <label for="Email">Email</label> 
 
      <input type="text" name="Email" id="Email" size="20" maxlength="20" required="required" /> 
 
      </p> 
 
      <p> 
 
      <label for="Password">Password</label> 
 
      <input type="text" name="Password" id="Password" size="20" maxlength="20" required="required" /> 
 
      </p> 
 
      <p> 
 
      <label for="ConPassword">Confirm Password</label> 
 
      <input type="text" name="ConPassword" id="ConPassword" size="20" maxlength="20" required="required" /> 
 
      </p> 
 
      <p> 
 
      <label for="Phone">Phone Number</label> 
 
      <input type="text" name="Phone" id="Phone" maxlength="12" size="12" placeholder="(03)92251515" /> 
 
      </p> 
 
      <input type="submit" value="Register Now!" /> 
 
      <input type="reset" value="Reset" /> 
 

 
     </fieldset> 
 

 
     </form>

回答

1

你在你的JavaScript有wrog名(應該是手機,而不是PHONENO):

if (!phonepatt1.test(Phone)) { 
    phonetest1 = false; 
} 
if (!phonepatt2.test(Phone)) { 
phonetest2 = false; 
}