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 / / 20144611174 ">  
The code is as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""> 
<html xmlns=""> 
<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;}  
<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
$("#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
$("# 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
$("#spnTip").addClass("spnBlur").html(" Incorrect mailbox format! "); 
else { //If the correct
function chkEmail(strEmail) { 
var vChk = /^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$/; 
if (!vChk.test(strEmail)) { 
return false; 
else { 
return true; 
<form id="form1" action="#"> 
<div id="email" class="divInit"> email : 
<span id="spnTip" class="spnInit"></span> 
<input type="text" id="txtEmail" class="txtInit" /> 

Related articles: