在做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>");
}
});
});

注:

  1. 对于对象type为text ,textarea,password的input标签可以用的验证类型ValidateType为:Int,Float,Email;
  2. 如果需要自定义错误提示信息:可以给标签添加ErrorMsg属性
  3. 表单验证要求验证属于该表单的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)的更多相关文章

  1. jQuery 表单验证插件 jQuery Validation Engine 使用

    jQuery 表单验证插件 jQuery Validation Engine 使用方式如下: 1.引入头文件(注意一定要把jQuery放在前面),指定使用 jQuery Validation Engi ...

  2. jQuery插件 -- 表单验证插件jquery.validate.js, jquery.metadata.js

    原文地址:http://blog.csdn.net/zzq58157383/article/details/7718352   最常使用JavaScript的场合就是表单的验证,而jQuery作为一个 ...

  3. jQuery插件 -- 表单验证插件jquery.validate.js

    最常使用JavaScript的场合就是表单的验证,而jQuery作为一个优秀的JavaScript库,也提供了一个优秀的表单验证插件----Validation.Validation是历史最悠久的jQ ...

  4. 表单验证插件jquery.validate.js

    最常使用JavaScript的场合就是表单的验证,而jQuery作为一个优秀的JavaScript库,也提供了一个优秀的表单验证插件----Validation.Validation是历史最悠久的jQ ...

  5. 表单验证插件jquery.validate的使用方法演示

    jQueryValidate表单验证效果 jquery.validate验证错误信息的样式控制 <!--validate验证插件的基础样式--> input.error{border: 1 ...

  6. 表单验证插件 jquery.validata 使用方法

    参考资料:http://www.runoob.com/jquery/jquery-plugin-validate.html 下载地址 jquery.validate插件的文档地址http://docs ...

  7. jq中的表单验证插件------jquery.validate

    今天我们来说一下表单验证,有人说我们在进行表单验证的时候使用正则来验证是非常麻烦的,现在我来给大家介绍一下表单验证的插件:jquery.validate.min.js 它是与jquery一起结合用来使 ...

  8. jquery表单验证插件 jquery.form.js ------转载

    Form插件,支持Ajax,支持Ajax文件上传,功能强大,基本满足日常应用. 1.JQuery框架软件包下载 文件: jquery.rar 大小: 29KB 下载: 下载 2.Form插件下载 文件 ...

  9. jquery表单验证插件 jquery.form.js-转

    来自:http://www.cnblogs.com/luluping/archive/2009/04/15/1436177.html Form插件,支持Ajax,支持Ajax文件上传,功能强大,基本满 ...

随机推荐

  1. IOS 手机助手及越狱助手推荐

    快装系列 1 快装助手(PC版) http://pc.kuaiapp.cn/ 2 快装越狱助手(PC版)http://www.kuaiapp.cn 3 快装商店(手机版)http://yueyu.ku ...

  2. Codeforces Round #336 (Div. 2)A. Saitama Destroys Hotel 水题

    A. Saitama Destroys Hotel 题目连接: http://www.codeforces.com/contest/608/problem/A Description Saitama ...

  3. asmack

    https://github.com/zhuqizhi/tlbs1409 https://github.com/maxting/IM_Project150126 https://github.com/ ...

  4. Gridview中绑定DropDownList

    1.页面代码                         <asp:TemplateField HeaderText="等级">                   ...

  5. [AngularJS+ GSAP] Greensock TimelineLite Animation Sequences

    TimelineLite is a piece of the Greensock TweenMax library that provides the ability to create sequen ...

  6. volatile synschonized的区别

    在一次面试中,被问到volatile与synschonized的区别,概念模模糊糊,今天做一个总结,加强自己的认识. 本文参考http://www.cnblogs.com/dolphin0520/p/ ...

  7. linux 内核调试方法

    http://my.oschina.net/fgq611/blog/113249 http://my.oschina.net/fgq611/blog/112929

  8. Linux内核高端内存 转

        Linux内核地址映射模型x86 CPU采用了段页式地址映射模型.进程代码中的地址为逻辑地址,经过段页式地址映射后,才真正访问物理内存. 段页式机制如下图.   Linux内核地址空间划分 通 ...

  9. 一款基于jquery的下拉点击改变背景图片

    今天给大家介绍一款基于jquery的下拉点击改变背景图片.单击右上角的图片,下拉显示可选择的背景图片,单击图片变为背景图.效果图下: 在线预览   源码下载 实现的代码. html代码: <a ...

  10. jquery ajax请求了两次问题

    页面有一个请求form: <form method="post" id="signin"> <label for="orderid& ...