javascript验证 - Javascript没有运行

时间:2014-10-19 09:17:44

标签: javascript html

我正在尝试验证输入,到目前为止我只创建了两个规则。一个用于测试电话号码,另一个用于测试密码输入是否相同。

我的问题是由于某些原因我的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 个答案:

答案 0 :(得分:1)

你的JavaScript中有wrog名称(应该是Phone而不是Phoneno):

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