Javascript在提交到php文件之前没有运行

Javascript not running before submitting to php file

本文关键字:运行 文件 提交 php Javascript      更新时间:2023-09-26

我正在学习更多关于PHP/Javascript,试图建立一个基本的网站。我已经把所有的东西都启动并运行了,但是没有注册验证之类的东西。我试图使用Javascript在提交之前验证表单,但它不适合我。这是我的页面:

<script language="JavaScript" type="text/javascript">
OK = false;
function validateEmail() {
    var x = document.forms["form1"]["myemail"].value;
    var atpos = x.indexOf("@");
    var dotpos = x.lastIndexOf(".");
    if(atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= x.length) {
        alert("Not a valid e-mail address");
        return false;
    }
}
function notShortUsername() {
    var x = document.forms["form1"]["myusername"].value;
    if(x.value.length < 6) {
        // set the focus to this input
        OK = false;
    }
    OK = true;
}
function notShortPassword() {
    var x = document.forms["form1"]["mypassword"].value;
    if(x.value.length < 6) {
        alert(helperMsg);
        elem.focus();
        // set the focus to this input
        OK = false;
    }
    OK = true;
}
function validate () {
    notShortUsername();
    notShortPassword();
    validateEmail();
    if(OK) {
        form.submit();
        return true;
    } else {
        //do something else
        alert("You did it wrong!");
        return false;
    }
} 
</script>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
  <tr>
  <form name="form1" method="post" action="registervalidation.php" onsubmit="return validate">
    <td>
      <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
        <tr>
          <td colspan="3"><strong>Member Login </strong></td>
        </tr>
        <tr>
          <td width="78">Username</td>
          <td width="6">:</td>
          <td width="294">
            <input name="myusername" type="text" id="myusername" />
          </td>
        </tr>
        <tr>
          <td>Password</td>
          <td>:</td>
          <td>
            <input name="mypassword" type="password" id="mypassword" />
          </td>
        </tr>
        <tr>
          <td>Email</td>
          <td>:</td>
          <td>
            <input name="myemail" type="text" id="myemail">
          </td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>
            <input type="submit" name="Submit" value="Complete Registration">
            <input type="submit" name="Submit" value="Cancel">
          </td>
        </tr>
      </table>
    </td>
  </form>
  </tr>
</table>

我也试过使用这个:http://docs.jquery.com/Plugins/Validation

它的问题是当条目失去焦点时它不会验证,并且仍然提交php。

onsubmit="return validate();"
不做

忘记();

改变这个:

var x = document.forms["form1"]["myusername"].value;
if(x.value.length < 6) {

:

var x = document.forms["form1"]["myusername"].value;
if(x.length < 6) {

:

var x = document.forms["form1"]["mypassword"].value;
if(x.value.length < 6) {

:

var x = document.forms["form1"]["mypassword"].value;
if(x.length < 6) {

在每种情况下,var x已经包含该值,因此您可以直接通过x.length

获得长度

在表单外测试验证函数submit:

<a href="#" onclick="validate();">test</a>

您将在控制台中看到一个错误,告诉您没有定义x。

Uncaught TypeError: Cannot read property 'length' of undefined

因此脚本停止执行并且永远不会返回false,这允许表单发布