输入框失去焦点时对输入信息进行JS验证
来源:互联网 发布:perfect illusion 知乎 编辑:程序博客网 时间:2024/05/17 23:47
以前写的JS验证是在用户输入信息完毕后点击提交时才对输入的信息进行JS验证。
当输入框失去焦点时即对输入框中的信息进行JS验证,及时有效的反馈出验证信息,使得项目具有更良好的用户体验。
下面以一个用户注册的页面为例。
首先是form表单部分:
<form name="myform" onsubmit="return check()" action="fun/logins_register" method="post"><table cellspacing="0" cellpadding="0" width="100%" border="0" height="296" id="table212"><tr><td width="20%" height="35" class="top_hui_text"><span class="login_txt"> 帐 号: </span></td><td width="34%" class="top_hui_text"><input class="editbox40" size="20" id="username" name="username"><font color="red">*</font><img style="display: none;" id="usernameRight" src="img/right.png" width="19" class="isRight" height="18"></img></td><td id="usernameError" width="46%" height="35" colspan="2" class="top_hui_text" style="display:none"><font color="red" size="-1">帐号格式不正确</font></td></tr><tr><td width="20%" height="35" class="top_hui_text"><span class="login_txt"> 密 码: </span></td><td class="top_hui_text"><input class="editbox4" type="password" size="20" id="userpassword" name="userpassword"> <font color="red">*</font> <img style="display: none;" id="userpasswordRight" class="isRight" src="img/right.png" width="19" height="18"></img></td><td id="userpasswordError" height="35" colspan="2" class="top_hui_text" style="display:none"><font color="red" size="-1">密码需6-16位</font></td></tr><tr><td width="20%" height="35" class="top_hui_text"><span class="login_txt"> 密码确认: </span></td><td class="top_hui_text"><input class="editbox4" type="password" size="20" id="userpassword1" name="userpassword1"> <font color="red">*</font> <img style="display: none;" id="userpassword1Right" class="isRight" src="img/right.png" width="19" height="18"></img></td><td id="userpassword1Error" height="35" colspan="2" class="top_hui_text" style="display:none"><font color="red" size="-1">两次密码不一致</font></td></tr><tr><td width="20%" height="35" class="top_hui_text"><span class="login_txt"> 邮 箱: </span></td><td class="top_hui_text"><input class="editbox4" size="20" id="useremail" name="useremail"> <img style="display: none;" id="useremailRight" class="isRight" src="img/right.png" width="19" height="18"></img></td><td id="useremailError" height="35" colspan="2" class="top_hui_text" style="display:none"><font color="red" size="-1">邮箱格式不正确</font></td></tr><tr><td width="20%" height="35" class="top_hui_text"><span class="login_txt"> QQ : </span></td><td class="top_hui_text"><input class="editbox4" size="20" id="userqq" name="userqq"> <img class="isRight" style="display: none;" id="userqqRight" src="img/right.png" width="19" height="18"></img></td><td id="userqqError" height="35" colspan="2" class="top_hui_text" style="display:none"><font color="red" size="-1">QQ号码格式不正确</font></td></tr><tr><td width="20%" height="35" class="top_hui_text"><span class="login_txt"> 手 机: </span></td><td class="top_hui_text"><input class="editbox4" size="20" id="userphone" name="userphone"> <img style="display: none;" id="userphoneRight" class="isRight" src="img/right.png" width="19" height="18"></img></td><td id="userphoneError" height="35" colspan="2" class="top_hui_text" style="display:none"><font color="red" size="-1">手机号码格式不正确</font></td></tr><tr><td width="20%" height="35"> </td><td class="top_hui_text"><input name="submit" type="submit" id="submit" class="button" value="注册" style="background-image: url('img/Submit_bg.gif');"></td></tr></table></form>
设计思想是,当输入框失去焦点时对输入框信息进行验证,验证成功则显示√的图片,验证失败则显示错误信息。
这些图片的class均为isRight,当以class获取的部分均显示出来的时候表示验证成功。
JS部分代码:
<script type="text/javascript">$(document).ready(function() {//username验证$("#username").focus(function() {$("#username").css("background-color", "#FFFFCC");});$("#username").blur(function() {var regtitle = /^[a-zA-Z][a-zA-Z0-9_]*$/;var uname = $("#username").val();$("#username").css("background-color", "white");if (uname.length <= 0 || !regtitle.test(uname)) {$("#usernameRight").hide();$("#usernameError").show();} else {$("#usernameRight").show();$("#usernameError").hide();}});//password验证$("#userpassword").focus(function() {$("#userpassword").css("background-color", "#FFFFCC");});$("#userpassword").blur(function(){var upassword = $("#userpassword").val();$("#userpassword").css("background-color", "white");if (upassword.length > 5 && upassword.length < 17) {$("#userpasswordRight").show();$("#userpasswordError").hide();} else {$("#userpasswordRight").hide();$("#userpasswordError").show();}});//password1验证$("#userpassword1").focus(function() {$("#userpassword1").css("background-color", "#FFFFCC");});$("#userpassword1").blur(function(){var upassword = $("#userpassword").val();var upassword1 = $("#userpassword1").val();$("#userpassword1").css("background-color", "white");if (upassword1 != upassword) {$("#userpassword1Right").hide();$("#userpassword1Error").show();} else {$("#userpassword1Right").show();$("#userpassword1Error").hide();}});//userphone验证$("#userphone").focus(function() {$("#userphone").css("background-color", "#FFFFCC");});$("#userphone").blur(function() {var regphone = /^(13[0-9]|15[0-9]|18[0-9]|16[0-9])\d{8}$/;var uphone = $("#userphone").val();$("#userphone").css("background-color", "white");if (uphone.length <= 0 || !regphone.test(uphone)) {$("#userphoneRight").hide();$("#userphoneError").show();} else {$("#userphoneRight").show();$("#userphoneError").hide();}});//usereamil验证$("#useremail").focus(function() {$("#useremail").css("background-color", "#FFFFCC");});$("#useremail").blur(function() {var regemail = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;var uemail = $("#useremail").val();$("#useremail").css("background-color", "white");if (uemail.length <= 0 || !regemail.test(uemail)) {$("#useremailRight").hide();$("#useremailError").show();} else {$("#useremailRight").show();$("#useremailError").hide();}});//userqq验证$("#userqq").focus(function() {$("#userqq").css("background-color", "#FFFFCC");});$("#userqq").blur(function() {var regQQ = /^\d{5,13}$/;var userqq = $("#userqq").val();$("#userqq").css("background-color", "white");if (userqq.length <= 0 || !regQQ.test(userqq)) {$("#userqqRight").hide();$("#userqqError").show();} else {$("#userqqRight").show();$("#userqqError").hide();}});});function check(){if($(".isRight").css("display")!='none'){if(confirm("确认注册吗?")){return true;}else{return false;}}else{return false;}}</script>
结果显示如下图:
- 输入框失去焦点时对输入信息进行JS验证
- JS小插件-对输入框获取与失去焦点时的操作
- 输入框的获取焦点、失去焦点
- 输入框获得焦点和失去焦点
- input 输入框获得/失去焦点时隐藏/显示文字
- input 输入框获得/失去焦点时隐藏/显示文字
- 当输入框失去焦点时隐藏软键盘
- Jquery输入框获得/失去焦点方式
- Android 让EditText输入框失去焦点
- Android-->输入框自动提示文本, 失去焦点自动输入
- jQuery实现输入框获取焦点和失去焦点
- easyui combo 派生类 combobox, combogrid 失去焦点后的输入框的数据验证问题
- 文本框失去焦点后js判断输入是否为数字
- 实现输入框得到焦点时边框颜色改变失去焦点时输入框变回原来的颜色
- JS 对输入框进行限制
- js在input失去焦点时对身份证、手机号、邮箱的合法性验证
- 当失去输入焦点 输入获得焦点后 文字值改变时 文字加亮后
- HTML中使得input输入框失去焦点
- strncat_s
- 在java web项目中慎用Executors以及非守护线程
- Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询
- Neo4J核心模型
- !!!Tomcat服务器监控[{$latestVersion}]版 for Win,Linux(转载)
- 输入框失去焦点时对输入信息进行JS验证
- 移植错误mach-mini2440.c array type has incomplete element type
- 计算机视觉中——流形学习
- Android移动应用界面的模板化设计
- 修改ssh默认端口号
- CI框架源码阅读---------Utf8.php
- configure: error: Popt libraries is required
- CarParts
- 如何修改F7字段所关联的业务单元