English 中文(简体)
提交前的审定表格
原标题:Validating form before submitting

I could submit a form and validate it using a simple anonymous function in javascript as,

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

最佳回答

http://ryancannon.com/ Design/cs_plus_js/cancelling-events-with-javascript”rel=“nofollow” with:

http://jsfiddle.net/pYYSZ/42/"rel=“nofollow> http://jsfiddle.net/pYYSZ/42/

问题回答

你把这个问题放在“Qu子”下,因此,我ll弃了ya子解决办法:

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

请注意,我使用了j Query 1.7 .on(功能,在此情况下,取代先前版本的.bind(>功能。

<>-Update-

http://jsfiddle.net/jasper/LmaYk/“rel=“nofollow”

类似情况

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




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签