JavaScript listens for text box carriage return events and filters text box spaces _javascript Tips

Source: Internet
Author: User

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.

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.