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.