dqrmkdu25623 2011-11-04 14:30
浏览 26
已采纳

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

I'm learning more about PHP/Javascript, trying to set up a basic website. I've got the full thing up and running, but there is no validation on registration and whatnot. I'm trying to use Javascript to validate the form before submitting it, but it's not working for me. Here is my page:

<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>

I've also tried using this: http://docs.jquery.com/Plugins/Validation

The issue with it is it won't validate when the item loses focus, and still submits the php anyways.

  • 写回答

3条回答 默认 最新

  • douxi5940 2011-11-04 14:32
    关注
    onsubmit="return validate();"
    

    Do not forget the ();

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么