简体   繁体   中英

JavaScript form validation not working

I have a very simple form with a very simple JavaScript validator.

I want an alert to popup if the first nae isnt filled out only it doesnt seem to be alerting and still submits.

I've uploaded a fiddle here http://jsfiddle.net/nvgMq/

You code is correct but it's jsfiddle's problem. After inspecting the jsfiddle's HTML page, I found out that the JavaScript code has become:

window.addEvent('load', function() {
    function validateForm()
     {
       var x=document.forms["subForm"]["ajijjt-ajijjt"].value

        if (x==null || x=="")
        {
            alert("First name must be filled out");
            return false;
        }
      }
});

So the function validateForm is no longer a global variable, it can not be accessed in the <form> 's onsubmit .

You can use code like:

window.validateForm = function() {}

This will create the global variable for validateForm .

Have you checked the value of x? Try putting a semi colon after the .value

var x=document.forms["subForm"]["ajijjt-ajijjt"].value;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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