在做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. [置顶] Android开发之MediaPlayerService服务详解(一)

    前面一节我们分析了Binder通信相关的两个重要类:ProcessState 和 IPCThreadState.ProcessState负责打开Binder 驱动,每个进程只有一个.而 IPCThre ...

  2. CloudStack的VO在调用setRemoved方法抛异常的原因

    今天在开发中发现一个问题,本来想对一个VO对象的removed值赋值,然后去update一下这条记录,一个最简单的set方法,但是在调用时直接抛异常了. 1: public void setRemov ...

  3. 【JavaScript】JavaScript中的陷阱大集合

    本文主要介绍怪异的Javascript,毋庸置疑,它绝对有怪异的一面.当软件开发者开始使用世界上使用最广泛的语言编写代码时,他们会在这个过 程中发现很多有趣的“特性”.即便是老练的Javascript ...

  4. IOS编程之多线程

    IOS编程之多线程 目录 概述——对多线程的理解 IOS中实现多线程的三种方式 NSThread 线程创建 线程的同步与锁 线程间的交互 线程的操作方法 NSOperation and NSOpera ...

  5. 推荐几个可以与PhoneGap很好搭配的UI框架

    - xui.js:可以被视作是jquery在phonegap上的替代品,挺好用的- jq.mobi:同上,不过体积比xui.js要大,一般还是用xui.js- jq.ui:jq.mobi配套的UI框架 ...

  6. 适用于各浏览器支持图片预览,无刷新异步上传js插件

    文件上传无疑是web应用中一个非常常用的功能,不管是PHP.jsp还是aspx.mvc等都会需要文件上传,但是众所周知当使用自带的文件上传功能时总会出现页面刷新的情况.当然现在有了html5这个好东西 ...

  7. Fedora 23如何安装LAMP服务器

    LAMP 是开源系统上 Web 服务器的梦幻组合.LAMP 是 Linux. Apache HTTP 服务. MySQL/MariaDB 数据库和 PHP. Perl 或 Python 的简称. 下面 ...

  8. LeetCode34 Search for a Range

    题目: Given a sorted array of integers, find the starting and ending position of a given target value. ...

  9. tachyon 集群容错

    集群容错就是HA.这次顺带也练一下hadoop的HA 环境: centos6.5+jdk1.7+hadoop2.2.0+tachyon0.5.0+zookeeper3.4.6 hadoop 192.1 ...

  10. 顺丰快递单号查询api对接(全代码)

    接口支持的消息接收方式:HTTP POST 请求方法的编码格式(utf-8):"application/x-www-form-urlencoded;charset=utf-8" 请 ...