The JavaScript text box is used to listen to the text box press ENTER event and filter text box spaces.

Source: Internet
Author: User

The JavaScript text box is used to listen to the text box press ENTER event and filter text box spaces.

This example describes how to listen to the text box carriage return event in JavaScript and filter text box spaces. Share it with you for your reference. The details are as follows:

<Script type = "text/javascript" language = "javascript"> var username = null; var password = null; // obtain the text box onload = function () {username = document. getElementById ("txtUserName"); password = document. getElementById ("txtPassWord");} // clear the text box function clearTxt () {username. value = ""; password. value = ""; username. focus (); // document. getElementById ('txtusername '). value = ""; // document. getElementById ('txtpassw Ord '). value = ""; // document. getElementById ('txtusername '). focus () ;}// determine function chkTxt () {// Delete the front and back spaces username. value = username. value. replace (/(^ \ s *) | (\ s * $)/g, ""); password. value = password. value. replace (/(^ \ s *) | (\ s * $)/g, ""); // empty if (username. value. length = 0) {alert ("Enter the user name! "); Username. focus () ;}else if (password. value. length = 0) {alert (" enter the password! "); Password. focus ();} else document. getElementById ("btnLogin "). click ();} // press enter to listen to function onkey () {if (window. event. keyCode = 13) {// document. all ["btnLogin"]. focus (); // if (document. activeElement. id = "aReset") // the id of the control that determines the current focus is aReset // {// document. getElementById ("aReset "). focus (); //} document. getElementById ("aLogin "). focus (); return false ;}}</script>

Css code:

<style type="text/css"> #btnLogin {  width: 0px;  height: 0px;  border-style: none;  background-color: White; } </style>

Html code:

<Body onkeydown = "onkey () "> // Add the carriage return listener to the body <form id =" login_form "name =" login_form "runat =" server "> <div> <label> User: </label> <input id = "txtUserName" runat = "server" name = "u_name" class = "input bold" type = "text"/> <label> password: </label> <input id = "txtPassWord" runat = "server" name = "u_pass" class = "input" type = "password"/> <a href = "javascript: chkTxt () "id =" aLogin "> OK </a> <% -- <a href =" javascript: document. forms ['login _ form']. reset () "> reset </a> -- %> <a href =" javascript: clearTxt () "id =" aReset "> reset </a> <asp: button ID = "btnLogin" runat = "server" Text = "" OnClick = "btnLogin_Click"/> </div> </form> </body>

I hope this article will help you design javascript programs.

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.