An example of an event application in a jquery text box is an input mailbox

  • 2020-03-30 02:50:40
  • OfStack

Event application in the text box: take the input mailbox as an example, as shown in the figure:
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201405/201405061116509.gif? 20144611174 ">  
The code is as follows:
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>jquery Event application in text box </title> 
<style type="text/css"> 
body{ font-size:13px;} 
 
.divInit{ width:390px; height:55px; line-height:55px; padding-left:20px;} 
.txtInit{ border:solid 1px #666; padding:3px; background-image:url('Images/bg_email_input.gif');} 
.spnInit{ width:179px; height:40px; line-height:40px; float:right; margin-top:8px; padding-left:10px; background-repeat:no-repeat;} 
 
.divBlur{ background-color:#FEEEC2;} 
.txtBlur{ border:solid 1px #666; padding:3px; background-image:url('Images/bg_email_input2.gif');} 
.spnBlur{ background-image:url('Images/bg_email_wrong.gif');} 
.divFocu{ background-color:#EDFFD5;}  
.spnSucc{ background-image:url('Images/pic_Email_ok.gif'); margin-top:20px;}  
</style> 
<script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script> 
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script> 
<script type="text/javascript"> 
$(function () { 
$("#txtEmail").trigger("focus"); //By default, the text box gets focus
$("#txtEmail").focus(function () { //The text box gets the focus event
$(this).removeClass("txtBlur").addClass("txtInit"); 
$("#email").removeClass("divBlur").addClass("divFocu"); 
$("#spnTip").removeClass("spnBlur").removeClass("spnSucc").html(" Please enter your usual email address! "); 
}); 
$("#txtEmail").blur(function () { //Text box missing focus event
var vTxt = $("#txtEmail").val(); 
if (vTxt.length == 0) { //Whether a mailbox is entered in the text box
$(this).removeClass("txtInit").addClass("txtBlur"); 
$("# email").removeClass("divFocu").addClass("divBlur"); 
$("#spnTip").addClass("spnBlur").html(" Email address cannot be empty! "); 
} 
else { //Check that the mailbox format is correct
if (!chkEmail(vTxt)) { //If it's not correct
$(this).removeClass("txtInit").addClass("txtBlur"); 
$("#email").removeClass("divFocu").addClass("divBlur"); 
$("#spnTip").addClass("spnBlur").html(" Incorrect mailbox format! "); 
} 
else { //If the correct
$(this).removeClass("txtBlur").addClass("txtInit"); 
$("#email").removeClass("divFocu"); 
$("#spnTip").removeClass("spnBlur").addClass("spnSucc").html(""); 
} 
} 
}); 
 
function chkEmail(strEmail) { 
var vChk = /^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$/; 
if (!vChk.test(strEmail)) { 
return false; 
} 
else { 
return true; 
} 
} 
}); 
</script> 
</head> 
<body> 
<form id="form1" action="#"> 
<div id="email" class="divInit"> email : 
<span id="spnTip" class="spnInit"></span> 
<input type="text" id="txtEmail" class="txtInit" /> 
</div> 
</form> 
</body> 
</html> 

Related articles: