简体   繁体   English

提交前验证表格

[英]Validating form before submitting

I could submit a form and validate it using a simple anonymous function in javascript as, 我可以提交表单并使用javascript中的简单匿名函数对其进行验证,

 document.getElementById('myForm').onsubmit = function() {
     if(document.getElementById('email').value == "") {
         document.getElementById('errormessage').innerHTML = "Please provide at least an email address";
         return false;
     } else {
         return true; // Note this
     }
 };

In the example above using anonymous function, I could simply assign the return value when the document is submitted and prevent the page from being submitted while in the example below I use addEventListener to add event when the form is submitted and it is further evaluated by validate function. 在上面的示例中使用匿名函数,我可以简单地在提交文档时分配返回值,并阻止页面提交,而在下面的示例中,我使用addEventListener在提交表单时添加事件,并由validate进一步评估功能。 I have also placed the return value to be true or false but this approach does not seem to work and the page gets submitted. 我也将返回值设置为true或false,但是这种方法似乎不起作用,并且页面已提交。 I think I am wrong in returning the value 我认为返回值是错误的

function addEventHandler(node, evt, func) {
    if(node.addEventListener)
        node.addEventListener(evt, func);
    else
        node.attachEvent("on" + evt, func);
}
function initializeAll() {
    addEventHandler(document.getElementById('myform'), 'submit', validate);
}
function validate() {
    if(document.getElementById('email').value == ""){
        document.getElementById("errormessage").innerHTML = "Please provide at least an email ";
        return false;
    } else {
        return true;
    }
}
addEventHandler(window, 'load', initializeAll);

How should I return value this way so as to prevent the form from submitting or submitting the form? 我应该如何以这种方式返回价值,以防止表单提交或提交表单?

You posted this question under jQuery, so I'll throw out a jQuery solution for ya: 您在jQuery下发布了这个问题,因此我将为您抛出jQuery解决方案:

$('#myform').on('submit', function () {
    if($('#email').val() == ""){
        $('#errormessage').html("Please provide at least an email ");
        return false;
    } else {
        return true;
    }
});

Note that I used the jQuery 1.7 .on() function which in this case replaces the .bind() function of earlier versions. 请注意,我使用了jQuery 1.7 .on()函数,在这种情况下,它替换了早期版本的.bind()函数。

--Update-- --Update--

Here is a jsfiddle of the above solution: http://jsfiddle.net/jasper/LmaYk/ 这是上述解决方案的jsfiddle: http : //jsfiddle.net/jasper/LmaYk/

I think your problem occurs because you are using the DOM Level 2 Event Registraton with addEventListener , so you can use event.preventDefault() for Firefox and event.returnValue=false to other browsers. 我认为发生您的问题是因为您将DOM Level 2事件注册表addEventListener一起使用 ,因此可以将event.preventDefault()用于Firefox,将event.returnValue=false用于其他浏览器。 Try with this demo: 试试这个演示:

http://jsfiddle.net/pYYSZ/42/ http://jsfiddle.net/pYYSZ/42/

用这样的东西

<form ...... onsubmit="return your validationFunctionThatReturnTruOrFalse() " >

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

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