JavaScript Mask carriage return submit Form

Source: Internet
Author: User

HTML uses input to prevent carriage return submission

By default, a single input box, regardless of the button's type= "Submit" or a type= "button" type, is submitted by carriage return.

1. When the type= "submit", no matter how many type= "text" input box, carriage return represents submission. (Submit)

2. When type= "button", and there are more than one input box, carriage return is not submitted. (Button)

3. Solve a single input box carriage return is to submit questions, refer to the 2nd: You can add a input= "text", hidden; The type is then a button.

JS prevents carriage return submission

The code is as follows Copy Code
function Ifenter () {
if (event.keycode==13) {
return false;
}else if (event.srcelement.type== "submit") {
Form1.submit ();
}
}
Document.onkeydown=ifenter;

or use jquery

The code is as follows Copy Code

//No press RETURN is submitted
      $ (document). Ready (function () {
        $ ("input[@type = ' text ']"). each (
            function () {
                $ ( This). KeyPress (function (e) {
                         var key = window.event? E.keycode:e.which;
                         if (key.tostring () = = "{
                                      return false;

}
});
});
})

or written directly in HTML.

The code is as follows Copy Code

<form name= "MyForm" action= "onkeydown=" if (event.keycode==13) {return false;} " >
<input Type=text name=user>
<input Type=text name=pass>
<input Type=button Name=mybutton value=submit onclick=myform.submit () >
</form>

This allows us to press ENTER will not be submitted, but click the Submit button can be submitted to understand

Related Article

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.