六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 71|回复: 0

JQuery 表单验证

[复制链接]

升级  32.67%

25

主题

25

主题

25

主题

秀才

Rank: 2

积分
99
 楼主| 发表于 2013-2-7 18:47:54 | 显示全部楼层 |阅读模式
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>Panel</title><style type="text/css"> 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(./img/reg3.gif) no-repeat 0 center;padding-left:25px;}.onSuccess{    background:#E9FBEB url(./img/reg4.gif) no-repeat 0 center;padding-left:25px;}.high{    color:red;}</style><script src="./jquery-1.3.1.js" type="text/javascript"></script><script type="text/javascript"> $(function(){$("form :input.required").each(function(){      var $required = $("<strong class='high'>*</strong>");      $(this).parent().append($required);});$('form :input').blur(function(){var $parent = $(this).parent();$parent.find(".formtips").remove();if($(this).is('#username')){if( this.value=="" || this.value.length < 6){var errorMsg = '请输入至少6位的用户名.';$parent.append('<span class="formtips onError">'+errorMsg+'</span>');}else{var okMsg = '输入正确.';$parent.append('<span class="formtips onSuccess">'+okMsg+'</span>');}}if($(this).is('#email')){if(this.value==""||(this.value!=""&&!/.+@.+\.[a-zA-Z]{2,4}$/.test(this.value))){var errorMsg = '请输入正确的E-Mail地址.';$parent.append('<span class="formtips onError">'+errorMsg+'</span>');}else{var okMsg = '输入正确.';$parent.append('<span class="formtips onSuccess">'+okMsg+'</span>');}}}).keyup(function(){$(this).triggerHandler("blur");}).focus(function(){$(this).triggerHandler("blur");});$('#send').click(function(){$("form .required:input").trigger('blur');var numError = $('form .onError').length;if(numError){return false;}alert("注册成功,密码已发到你的邮箱,请查收.");});$('#res').click(function(){$(".formtips").remove});});</script></head><body><form action="" method="post">     <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> 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表