This article describes how JavaScript listens for text box carriage return events and filters text box spaces. Share to everyone for your reference. as follows:
<script type= "Text/javascript" language= "JavaScript" > var username = null;
var password = null;
Get text Box onload = function () {username = document.getElementById ("txtUserName");
Password = document.getElementById ("Txtpassword");
//Empty text box function Cleartxt () {username.value = "";
Password.value = "";
Username.focus ();
document.getElementById (' txtUserName '). value= "";
document.getElementById (' Txtpassword '). value= "";
document.getElementById (' txtUserName '). focus (); }//Determine function Chktxt () {//delete before and after space Username.value = Username.value.replace (/(^\s*) | (
\s*$)/g, ""); Password.value = Password.value.replace (/(^\s*) | (
\s*$)/g, "");
Sentence null if (Username.value.length = = 0) {alert ("Please enter username!");
Username.focus ();
else if (password.value.length = 0) {alert ("Please enter password!");
Password.focus ();
else document.getElementById ("Btnlogin"). Click (); //Carriage return Listener function OnKey () {if (window.event.keycode==13) {//document.all["Btnlogin"].focus ();//if (document.ac TivEelement.id = "Areset")//Determine the ID of the control with the current focus is areset//{//document.getElementById ("Areset"). Focal ();//} Documen
T.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 ()" >//The return monitor into 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"
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"
Server "
text=" "onclick=" Btnlogin_click "/>
</div>
</form>
</body>
I hope this article will help you with your JavaScript programming.