最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Form validation not running onsubmit - Stack Overflow

programmeradmin1浏览0评论

I am trying to run a form validation on an HTML form to make sure the person has filled out the two fields. I put together a JSFiddle here:

/

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. The two fields are name "emailaddress" and "Name" respectively and it looks like I'm checking their values correctly. 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.

I am trying to run a form validation on an HTML form to make sure the person has filled out the two fields. I put together a JSFiddle here:

http://jsfiddle/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. The two fields are name "emailaddress" and "Name" respectively and it looks like I'm checking their values correctly. 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.

Share Improve this question edited Dec 2, 2012 at 17:47 MillerMedia asked Dec 2, 2012 at 17:15 MillerMediaMillerMedia 3,66118 gold badges75 silver badges154 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

the problem is that you haven't defined ValidateForm in the Head section. i have edited the jsfiddle.check demo.

I can see two problem:

First, what is echeck? If I take that code away it will work. Second you should not do return after your control of the nameID.value. If you do that your control of the emailID.value will not be done.

To piggyback on Behnam's answer, it is because ValidateForm isn't defined in the head section as he said. But here's how to fix it:

In jsfiddle, in the upper left area in Frameworks & Extensions, set it to no-wrap . Then update and set as base to save your changes.

发布评论

评论列表(0)

  1. 暂无评论