js:
$("#email").blur(function(){ //获取id相应的元素的值,去掉其左右的空格 var email = $.trim($('#email').val()); //验证邮箱格式的js正則表達式 var isEmail = /^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$/; //清空显示层中的数据 $("#emailMess").html(""); if(email == ""){ $("#emailMess").html("<font color='red'>"+"邮箱不能为空"+"</font>"); } else if(!(isEmail.test(email))){ $("#emailMess").html("<font color='red'>"+"邮箱格式不对"+"</font>"); } else{ //此处能够操作向后台发送json数据,然后返回验证结果 }
html:
<script type="text/javascript" src="js/jquery-1.10.1.js"></script> <div class="li"> <label for="email" class="label"><span class="flag">* </span>邮箱:</label> <input type="text" name="userInfo.email" id="email" placeholder="请输入有效的邮箱" size="30" maxlength="25"/><label id="emailMess" class="label_mess"></label> </div>