简单的表单验证插件(Jquery)
在做web开发的时候经常遇到表单验证问题,表单验证一般有客户端验证和服务器端验证,这个验证插件仅仅能满足我的项目中的基本需求的。
Validate_Tools.js
function Validate_Text(obj){
return $.trim(obj.value) != "";
} function Validate_Select(obj){
return $.trim(obj.value) != "";
} function Validate_List(obj){
var flag = false; $(obj).find("input").each(function(){
if(this.checked){
flag = true;
return false;
}
}); return flag;
} function Validate_Expression(objValue, reg){
return $.trim(objValue) == "" ? false : new RegExp(reg).test(objValue);
} function Validate_Obj(obj) {
var flag = false;
var errorMsg = "";
var objType = $(obj).attr("type");
var objTitle = $(obj).parent(0).prev().text().replace(":", "").replace(":", ""); try{
if(objType == "text" || objType == "textarea" || objType == "password"){
var validateType = $(obj).attr("ValidateType");
switch (validateType){
case "Int":
flag = Validate_Expression(obj.value, "^[0-9]*$");
if (!flag) {
if ($.trim(obj.value) == "") {
errorMsg = objTitle + "不能为空!";
}
else {
errorMsg = objTitle + "格式有误,请填写正确的格式!";
}
}
break;
case "Float":
flag = Validate_Expression(obj.value, "^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$");
if (!flag) {
if ($.trim(obj.value) == "") {
errorMsg = objTitle + "不能为空!";
}
else {
errorMsg = objTitle + "格式有误,请填写正确的格式!";
}
}
break;
case "Email":
flag = Validate_Expression(obj.value, "^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$");
if (!flag) {
if ($.trim(obj.value) == "") {
errorMsg = objTitle + "不能为空!";
}
else {
errorMsg = objTitle + "格式有误,请填写正确的邮件格式!";
}
}
break;
default:
var regularExpression = $(obj).attr("ValidateExpression");
if (regularExpression != undefined && regularExpression != "") {
flag = Validate_Expression(obj.value, regularExpression);
if (!flag) {
if ($.trim(obj.value) == "") {
errorMsg = objTitle + "不能为空!";
}
else {
errorMsg = objTitle + "格式有误!";
}
}
}
else {
flag = Validate_Text(obj);
if (!flag) {
errorMsg = objTitle + "不能为空!";
}
}
break;
}
}
else if(objType == "select-one"){
flag = Validate_Select(obj);
if (!flag) {
errorMsg = "请选择" + objTitle + "!";
}
}
else if(objType == "file"){
flag = Validate_Text(obj);
if (!flag) {
errorMsg = "请选择上传文件" + objTitle + "!";
}
}
else{
flag = Validate_List(obj);
if (!flag) {
errorMsg = "请选择" + objTitle + "!";
}
} if(!flag){
if($(obj).attr("ErrorMsg") != undefined && $(obj).attr("ErrorMsg") != ""){
errorMsg = $(obj).attr("ErrorMsg");
} alert(errorMsg);
try{
obj.focus();
}
catch(e){
}
return flag;
}
}
catch(e){
alert(e.description);
flag = false;
return flag;
} return flag;
} function Validate_Form(){
var flag = true; try {
$("*[ValidateType]").each(function () {
flag = Validate_Obj(this); if (!flag) {
return flag;
}
});
}
catch (e) {
alert(e.description);
flag = false;
} return flag;
} function Validate_Group(group) {
var flag = true; try {
$("*[ValidateGroup]").each(function () {
if ($(this).attr("type") != "submit") {
if ($(this).attr("ValidateGroup") == group) {
flag = Validate_Obj(this); if (!flag) {
return flag;
}
}
}
});
}
catch (e) {
alert(e.description);
flag = false;
} return flag;
} $(function () {
$("input[type='submit']").each(function () {
if ($(this).attr("ValidateGroup") != undefined && $(this).attr("ValidateGroup") != "") {
$(this).click(function () {
return Validate_Group($(this).attr("ValidateGroup"));
});
}
});
//添加必填提示
$("*[ValidateType]").each(function () {
if ($(this).attr("type") != "submit") {
$(this).parent(0).append("<span style='color:red'>*</span>");
}
});
});
注:
- 对于对象type为text ,textarea,password的input标签可以用的验证类型ValidateType为:Int,Float,Email;
- 如果需要自定义错误提示信息:可以给标签添加ErrorMsg属性
- 表单验证要求验证属于该表单的HTML标签,给属于同一个表单的标签添加ValidateGroup属性,submit按钮也需要添加ValidateGroup,要求同一表单中的input标签和submit 按钮的ValidateGroup属性值相同
用法如下面的代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ProductQuotationEdit.aspx.cs" Inherits="Trade.Web.Product.ProductQuotationEdit" %> <!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 runat="server"><title>供应商报价</title>
<link href="../Styles/StyleSheet.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="../Styles/AutoComplete/jquery.autocomplete.css" type="text/css" />
<script type="text/javascript" src="../Scripts/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="../Scripts/Validate/Validate_Tools.js"></script>
<script src="../Scripts/AutoComplete/jquery.autocomplete.min.js" type="text/javascript"></script>
<script type="text/javascript"> function returnValue() { try { parent.closeDiv(); } catch (e) { alert(e.message); } } </script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="table_edit">
<tr>
<th>供应商: </th>
<td>
<asp:TextBox ID="ID" runat="server" Visible="false"></asp:TextBox>
<asp:TextBox ID="ProductID" runat="server" Visible="false"></asp:TextBox>
</td>
<th>报价日期: </th>
<td>
<asp:TextBox ID="QuotationDate" runat="server" ValidateGroup="Supplier" ValidateType="Text"></asp:TextBox>
</td>
</tr>
<tr>
<th>供应商货号: </th>
<td>
<asp:TextBox ID="SupplierItemNo" runat="server" ValidateGroup="Supplier" ValidateType="Text"></asp:TextBox>
</td>
<th>币种: </th>
<td>
<asp:DropDownList ID="Currency" runat="server" ValidateGroup="Supplier" ValidateType="Text"></asp:DropDownList>
</td>
</tr>
<tr>
<th>单价: </th>
<td>
<asp:TextBox ID="Price" runat="server" ValidateGroup="Supplier" ValidateType="Float"></asp:TextBox>
</td>
<th>起订量: </th>
<td>
<asp:TextBox ID="MiniOrderQty" runat="server" ValidateGroup="Supplier" ValidateType="Int"></asp:TextBox>
</td>
</tr>
<tr>
<th>是否开票: </th>
<td>
<asp:DropDownList ID="IsBilling" runat="server" ValidateGroup="Supplier" ValidateType="Text">
<asp:ListItem Text="是" Value=""></asp:ListItem>
<asp:ListItem Text="否" Value=""></asp:ListItem>
</asp:DropDownList>
</td>
<th>是否含税: </th>
<td>
<asp:DropDownList ID="IsTax" runat="server" ValidateGroup="Supplier" ValidateType="Text">
<asp:ListItem Text="是" Value=""></asp:ListItem>
<asp:ListItem Text="否" Value=""></asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<th>备注: </th>
<td colspan="">
<asp:TextBox ID="Remark" runat="server" TextMode="MultiLine"></asp:TextBox>
</td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="btnSave" runat="server" Text="保 存" ValidateGroup="Supplier" OnClick="btnSave_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
注:由于jquery1.4.4可以取得select标签的type属性为"select-one",
但是jquery1.7.1版本获取不到select标签的type属性,所以可以给select添加type="select-one" 。
简单的表单验证插件(Jquery)的更多相关文章
- jQuery 表单验证插件 jQuery Validation Engine 使用
jQuery 表单验证插件 jQuery Validation Engine 使用方式如下: 1.引入头文件(注意一定要把jQuery放在前面),指定使用 jQuery Validation Engi ...
- jQuery插件 -- 表单验证插件jquery.validate.js, jquery.metadata.js
原文地址:http://blog.csdn.net/zzq58157383/article/details/7718352 最常使用JavaScript的场合就是表单的验证,而jQuery作为一个 ...
- jQuery插件 -- 表单验证插件jquery.validate.js
最常使用JavaScript的场合就是表单的验证,而jQuery作为一个优秀的JavaScript库,也提供了一个优秀的表单验证插件----Validation.Validation是历史最悠久的jQ ...
- 表单验证插件jquery.validate.js
最常使用JavaScript的场合就是表单的验证,而jQuery作为一个优秀的JavaScript库,也提供了一个优秀的表单验证插件----Validation.Validation是历史最悠久的jQ ...
- 表单验证插件jquery.validate的使用方法演示
jQueryValidate表单验证效果 jquery.validate验证错误信息的样式控制 <!--validate验证插件的基础样式--> input.error{border: 1 ...
- 表单验证插件 jquery.validata 使用方法
参考资料:http://www.runoob.com/jquery/jquery-plugin-validate.html 下载地址 jquery.validate插件的文档地址http://docs ...
- jq中的表单验证插件------jquery.validate
今天我们来说一下表单验证,有人说我们在进行表单验证的时候使用正则来验证是非常麻烦的,现在我来给大家介绍一下表单验证的插件:jquery.validate.min.js 它是与jquery一起结合用来使 ...
- jquery表单验证插件 jquery.form.js ------转载
Form插件,支持Ajax,支持Ajax文件上传,功能强大,基本满足日常应用. 1.JQuery框架软件包下载 文件: jquery.rar 大小: 29KB 下载: 下载 2.Form插件下载 文件 ...
- jquery表单验证插件 jquery.form.js-转
来自:http://www.cnblogs.com/luluping/archive/2009/04/15/1436177.html Form插件,支持Ajax,支持Ajax文件上传,功能强大,基本满 ...
随机推荐
- codechef Arranging Cup-cakes题解
Arranging Cup-cakes Our Chef is catering for a big corporate office party and is busy preparing diff ...
- Could not load the assembly 'App_Web_cwclgcuu'. Make sure that it is compiled before accessing the page.
将网站迁移到windows server 2012 R2(64 bit), IIS 6.2(build 9200)上,爆出这个错误. 解决:右键相应的application pool,选择“Set A ...
- 在XAF应用程序使用现有的数据库?
https://documentation.devexpress.com/#Xaf/CustomDocument3061
- volatile synschonized的区别
在一次面试中,被问到volatile与synschonized的区别,概念模模糊糊,今天做一个总结,加强自己的认识. 本文参考http://www.cnblogs.com/dolphin0520/p/ ...
- 架构设计:负载均衡层设计方案(7)——LVS + Keepalived + Nginx安装及配置
1.概述 上篇文章<架构设计:负载均衡层设计方案(6)——Nginx + Keepalived构建高可用的负载层>(http://blog.csdn.net/yinwenjie/artic ...
- SquashFs工具制作
下面是SquashFs.tar.gz的下载地址,解压后,直接执行其中的Makefile即可. SquashFs.tar.gz 说明: 制作squashfs格式的压缩镜像: ./mksquashfs . ...
- Java再学习——栈(stack)和堆(heap)
一.内存分配的策略 按照编译原理的观点,程序运行时的内存分配有三种策略,分别是静态的,栈式的,和堆式的. 静态存储分配是指在编译时就能确定每个数据目标在运行时刻的存储空间需求,因而在编译时就可以给他们 ...
- jQuery layer[页面弹出框]
常见接口如下: 方法名 描述 $.layer({}) 核心接口,参数是一个对象,对象属性参见上述列表.诸如layer.alert()之类的为$.layer()的包装方法. layer.v 获取版本号. ...
- PN-Traniger
首先先从Bezier说起: 一条直线上有两个端点,P0和P1,那么直线可以写成 y = kx+b ,其实也就是P(t) = (1-t)P0 + P1 (这是个插值函数),(小注,我时常把这两个 ...
- 二手奢侈品电商Vestiaire Collective融资2000万美元
巴黎奢侈品电商Vestiaire Collective获得了2000万美元的C轮融资,投资方包括知名出版集团Condé Nast.Idinvest.Balderton和Ventech,其中Condé ...