简单的表单验证插件(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文件上传,功能强大,基本满 ...
随机推荐
- bootstrap适配移动端
上次在pythonanywhere上挂上去的页面,是这个样子的 而在手机上看是这个样子的 总之简直不能看= = 看了一下学校几个微信公众号的页面.都是用的bootstrap,好吧我也去试试看好了. 在 ...
- win8 企业版 安装 .net2.0 .net 3.5
Windows 8 默认集成 .Net Framework 4.5,因此运行一些基于3.5或以前版本的程序时会弹出这个提示. 2012-3-2 15:24 上传 下载附件 (23.91 KB) 这 ...
- Windows创建自动化任务
Windows创建自动化任务使得开机就打开相应的Python目录 1:计算机管理 2:找到任务计划程序 3:创建基本任务 4:任务触发器 5: 建立bat执行文件 start "" ...
- hihocoder #1178 : 计数 暴力
#1178 : 计数 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/problemset/problem/1178 ...
- linux下的文件操作——批量重命名
概述:在日常工作中,我们经常需要对一批文件进行重命名操作,例如将所有的jpg文件改成bnp,将名字中的1改成one,等等.文本主要为你讲解如何实现这些操作 1.删除所有的 .bak 后缀: renam ...
- 关于android 图像格式问题
这算是篇总结吧.6月份开始做的一个android上的ar项目结束了.我做的部分是二维码识别和图像识别的预处理.这个项目虽然很累,但是让我学到了很多东西,特别是严格的编码规则,和java代码的效率优化, ...
- php heredoc 与 nowdoc
php heredoc 与 nowdoc heredoc 结构 heredoc 句法结构:<<<.在该运算符之后要提供一个标识符,然后换行.接下来是字符串本身,最后要用前面定义的标识 ...
- CGI,FastCGI,PHP-CGI与PHP-FPM(转)
http://www.cnblogs.com/zl0372/articles/php_4.html CGI CGI全称是“公共网关接口”(Common Gateway Interface),HTTP服 ...
- Linux用户空间与内核空间(理解高端内存)
Linux 操作系统和驱动程序运行在内核空间,应用程序运行在用户空间,两者不能简单地使用指针传递数据,因为Linux使用的虚拟内存机制,用户空间的数据可能被换出,当内核空间使用用户空间指针时,对应的数 ...
- 解决fonts.gstatic.com无法访问
最近很多google的服务又在大陆地区受限了,原因不做过多讨论.屏蔽这些服务不仅仅意味着gmail,谷歌学术等方便的工具使用受到限制,更意味着很多寄托于google的web服务无法使用.wordpre ...