Recently, I encountered a problem with the submit () conflict with the onsubmit () event when I was doing the verification, and would like to add a submit () to the a tag to commit the form, and then add the OnSubmit event trigger validation method to the form. The results do not work, and finally you can only choose to submit the form in Java script. The scheme is as follows:
Set the form header as follows: The ID is added to get the form object in the Java Script script
<method= "POST" action= "Adminservlet" ID= " LoginForm ">
Add the onclick () event to the a tag to trigger the Java Script Form validation method Loginlayoutvalidate ()
<href= "javascript:void (0)" onClick= "loginlayoutvalidate ()" />
functionloginlayoutvalidate () {varUsername=document.getelementbyid ("UserName"); varpassword= document.getElementById ("PassWord"); varValidatecode=document.getelementbyid ("Validatecode"); if(UserName.value.trim () = = "") {alert ("User name cannot be empty"); return ; } Else if(PassWord.value.trim () = = "") {alert ("Password cannot be empty"); return ; } Else if(ValidateCode.value.trim () = = "") {alert ("Please enter the verification code"); return ; } Else{document.getElementById ("LoginForm"). Submit (); } }
Example:
<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <title>Document</title> <Script> functionSubmitcheck () {if(document.getElementById ('name'). Value== "') {alert ('You must enter a website name! '); document.getElementById ('name'). focus (); return false; } Else if(document.getElementById ('Ftp_url'). Value== "') {alert ('You must enter an FTP address! '); document.getElementById ('Ftp_url'). focus (); return false; } Else{document.getElementById ('fm_1'). Submit (); } } </Script></Head><Body> <formAction="#"ID= "Fm_1"name= "Fm_1"> <inputtype= "text"ID= "Name"name= "Name"Required= "Required">* <BR> <inputtype= "text"ID= "Ftp_url"name= "Ftp_url">* <BR> <inputtype= "Submit"value= "Submit"> <BR> <inputtype= "Reset"value= "Reset"> <BR> <ahref= "javascript:void (0)"onclick= "Checkform ()">Submit</a> <BR><ahref="#"onclick= "Document.fm_1.reset ()">Reset</a> </form></Body></HTML>
A label pre-commit verification