A label pre-commit verification

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.