This article is a mirror article of machine translation, please click here to jump to the original article.

View: 12055|Reply: 0

[HTML/HTML5] onsubmit event for form - the best way to implement validation before form submission

[Copy link]
Posted on 12/27/2015 11:39:08 AM | | |
Today I encountered a problem, the page includes a lot of public pages (all without form), and many places in it are self-submitted (add a type="submit" field to the page, and then write sumbit.click in js to execute the commit). But I need to verify whether the filling in the form meets the requirements before each submission, and if I add a call to the validation function to each submission, it will be a lot of work.
    I need to do something about it, which is to verify the form uniformly before submitting it. From the body has the event onbeforeunload before the page is closed, it is inferred that the form should also have a pre-commit eventonsubmit。 The function of this event is to automate the execution of the specified thing before the form is submitted.
    It should be noted that the return in onsubmit="return check()" must be added, otherwise even if the return value of check is false, it will still be submitted. That is, onsubmit="return false" is not to execute the commit; onsubmit="return true" or onsubmit="return" both execute the commit;
------------------------------------------
The main page is as follows:
<HTML>
<HEAD>
<scrip{filter}t type="text/javascrip{filter}t">
//form提交前,验证:营销活动最少选择两个
function check(){
    if(validation condition fails) {
        return false;
    }else{
        return true; You can also not write this return value, and you can submit it directly
    }
}
</scrip{filter}t>
</HEAD>
<body>
<form id="queryForm" onsubmit="return check()" action="<%=request.getContextPath()%>/FocReportServlet">
<jsp:include flush="true" page="../pub.jsp"/>
<jsp:include flush="true" page="../condition.jsp"/>
<jsp:include flush="true" page="condition_marketing.jsp"/>
<jsp:include flush="true" page="contrastChart.jsp"/>
<jsp:include flush="true" page="../pagectrl.jsp"/>
...
<input type="submit" id="submit" name="submit" style="visibility:hidden"/>
<HTML>
-----------------------------------
include page:
<img src="<%=request.getContextPath()%>/focReport/images/control_play_blue.png" style="cursor:hand"#323e32" face="simsun">submit.click()" alt="Click to submit analysis" >





Previous:What an algorithm, I have been depressed for several days.
Next:Delegate values between Winform forms
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com