Unable to validate solution with Ajax before submitting form

Source: Internet
Author: User

Bloggers in a small project, to implement a registration form without a refresh to verify the user name or password, but found that no matter how the first pass Ajax verification before the form is not submitted.

For example: a simple validation function

1 function Check () {2     $.post ("#name") ("xxx.php", {user:user}function(msg) {3      if(msg) {returnfalse;} 4 })5 }
1 <form class= "form"Method= "POST"Action= "xxx.php"onsubmit= "return check ()">2 <inputtype= "text"name= "User"ID= "User" />3 <Buttontype= "Submit"value= "Submit"/>4 </form>

Submissions will find that the form is submitted directly without executing the events in the $.post.

There are netizens to solve this:

1, with Blur, after the text box loses focus, transmits the data and verifies, returns the result.

Cons: If the user's browser logs the form information, the user no longer clicks the text box, or the text box cannot be validated after submission.

2, simply do not have to submit, the button to change the type of button, and then add a click function onclick= "Check ()" If the verification through the $ ("form"). Submit () Submission Form

Disadvantage: Cannot use HTML5 authentication.

Bloggers have collected these two ways, if there is a different way, may wish to exchange OH.

Unable to validate solution with Ajax before submitting form

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.