简体   繁体   English

表单验证未运行onsubmit

[英]Form validation not running onsubmit

I am trying to run a form validation on an HTML form to make sure the person has filled out the two fields. 我正在尝试在HTML表单上运行表单验证,以确保该人填写了两个字段。 I put together a JSFiddle here: 我把JSFiddle放在一起:

http://jsfiddle.net/yCqqj/ http://jsfiddle.net/yCqqj/

It has worked previously for me and I don't know what's going on now. 它以前对我有用,我不知道现在发生了什么。 I made a JSFiddle to pull it out of my site to make sure nothing else was conflicting with it but I'm realizing now that it's something in the code. 我创建了一个JSFiddle来将它从我的网站中删除,以确保没有其他任何东西与它发生冲突,但我现在意识到它是代码中的东西。 The two fields are name "emailaddress" and "Name" respectively and it looks like I'm checking their values correctly. 这两个字段分别是名称“emailaddress”和“Name”,看起来我正在检查它们的值。 Basically, I'm looking for an alert (and NOT a submittal of the form) if they've left either field blank. 基本上,如果他们将任何一个字段留空,我正在寻找警报(而不是表格的提交)。 Thanks for helping me track down the problem. 感谢您帮助我追踪问题。 I appreciate it. 我很感激。

function ValidateForm(){
    var emailID=document.MailingList.emailaddress
    var nameID=document.MailingList.Name

        if ((nameID.value==null)||(nameID.value=="")){
        alert("Please Enter your Name");
        nameID.focus();
        return false;
    }

    if ((emailID.value==null)||(emailID.value=="")){
        alert("Please Enter your Email ID");
        emailID.focus();
        return false;
    }
 }

​ Even though there is not return true, the form still submits. 即使没有返回true,表单仍然提交。

the problem is that you haven't defined ValidateForm in the Head section. 问题是你没有在Head部分定义ValidateForm。 i have edited the jsfiddle.check demo . 我编辑了jsfiddle.check 演示

I can see two problem: 我可以看到两个问题:

First, what is echeck? 首先,echeck是什么? If I take that code away it will work. 如果我把那个代码拿走它就行了。 Second you should not do return after your control of the nameID.value. 其次,您在控制nameID.value后不应该返回。 If you do that your control of the emailID.value will not be done. 如果您这样做,您将无法控制emailID.value。

To piggyback on Behnam's answer, it is because ValidateForm isn't defined in the head section as he said. 根据Behnam的回答,这是因为正如他所说,ValidateForm没有在头部中定义。 But here's how to fix it: 但这是如何解决它:

In jsfiddle, in the upper left area in Frameworks & Extensions, set it to no-wrap . 在jsfiddle中,在Frameworks&Extensions的左上区域中,将其设置为no-wrap。 Then update and set as base to save your changes. 然后更新并设置为基础以保存更改。

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM