运用jQuery写的验证表单(实例讲解)
2019/6/27 20:50:21
本文主要是介绍运用jQuery写的验证表单(实例讲解),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
//运用jQuery写的验证表单
<!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> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <script src="js/jquery-1.3.1.js"></script> <script src="js/lib/jquery.validate.js"></script> <script src="js/lib/jquery.validate.messages_cn.js"></script> <style> body { font: 12px/19px Arial, Helvetica, sans-serif; color: #666; } form div { margin: 5px 0; } .int label { float: left; width: 100px; text-align: right; } .int input { padding: 1px 1px; border: 1px solid #ccc; height: 16px; } .sub { padding-left: 100px; } .sub input { margin-right: 10px; } .formtips { width: 200px; margin: 2px; padding: 2px; } .onError { background: #FFE0E9 url(images/reg3.gif) no-repeat 0 center; padding-left: 25px; } .onSuccess { background: #E9FBEB url(images/reg4.gif) no-repeat 0 center; padding-left: 25px; } .high { color: red; } </style> <script> $(function () { $("form :input.required").each(function () { var $required = $("<strong class='high'>*</strong>"); $(this).parent().append($required); }) $("form :input").blur(function () { var $parent = $(this).parent(); $(".formtips").remove(); if ($(this).is("#username")) { if (this.value == '' || this.value.length < 6) { var errmsg = '请输入正确的格式.'; $parent.append('<span class="formtips onError">' + errmsg + '</span>') } else { var sucssemsg = '格式输入正确'; $parent.append('<span class="formtips onSuccess">' + sucssemsg + '</span>') } } if ($(this).is("#email")) { //邮箱格式正则表达式的用法 if (this.value == '' || ( this.value != "" && !/.+@.+\.[a-zA-Z]{2,4}$/.test(this.value) )) { var errmsg = '请输入正确的格式.'; $parent.append('<span class="formtips onError">' + errmsg + '</span>') } else { var sucssemsg = '格式输入正确'; $parent.append('<span class="formtips onSuccess">' + sucssemsg + '</span>') } } }) $("#send").click(function () { var onerr = $(this).parent().siblings().children().hasClass("onError");//出错就会添加class为onerror的元素 if(onerr) { return false; } else { alert("你已经注册成功了"); } } ) $("#reset").click(function () { $(".formtips").remove(); }) }) </script> </head> <body> <form method="post" action=""> <div class="int"> <label for="username">用户名:</label> <input type="text" id="username" class="required"/> </div> <div class="int"> <label for="email">邮箱:</label> <input type="text" id="email" class="required"/> </div> <div class="int"> <label for="personinfo">个人资料:</label> <input type="text" id="personinfo"/> </div> <div class="sub"> <input type="submit" value="提交" id="send"/><input type="reset" id="res"/> </div> </form> </body> </html>
以上这篇运用jQuery写的验证表单(实例讲解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持找一找教程网。
这篇关于运用jQuery写的验证表单(实例讲解)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-04package.json 文件位置在哪?-icode9专业技术文章分享
- 2024-10-01Craco.js学习:从入门到实践指南
- 2024-10-01Create-React-App学习:入门与实践指南
- 2024-10-01CSS-in-JS学习:从入门到实践指南
- 2024-09-30JSX语法学习:从入门到初步掌握
- 2024-09-30Mock.js学习:入门教程与实战演练
- 2024-09-30React Hooks学习:从入门到实践
- 2024-09-30受控组件学习:React中的基础入门教程
- 2024-09-29JS定时器教程:初学者必看指南
- 2024-09-29JS对象教程:初学者的全面指南