ValidationSugar表单验证框架的优点:
1、支持javascript端和后端的双重验证 (前端目前依赖于jquery.validate.js,也可以自已扩展)
2、代码简洁
3、调用方便
4、功能齐全
使用方法:
新建初始化类,将所有需要验证的在该类进行初始化,语法相当简洁并且可以统一管理,写完这个类你的验证就完成了70%
函数介绍:
Add 默认类型(邮件、手机、QQ等)
AddRegex 正则验证 在Add无法满足情部下使用
addFunc 使用js函数进行验证,一般用于业逻辑的验证 ,功能非常强大,可以满足各种验证(注意:addFunc函数验证后 后台需要重新验证,所以能用上两种方法验证的,尽量使用上面的)
using System;using System.Collections.Generic;using System.Linq;using System.Web;using SyntacticSugar;namespace ValidationSuarMVC.Models{ public class Validates { public static void Init() { //login ValidationSugar.Init(PageKeys.LOGIN_KEY, ValidationSugar.CreateOptionItem().Set("userName", true/*是否必填*/, "用户名").AddRegex("[a-z,A-Z].*", "用户名必须以字母开头").AddRegex(".{5,15}", "长度为5-15字符").AddFunc("checkUserName", "用户名不存在,输入 admin1 试试").ToOptionItem(), ValidationSugar.CreateOptionItem().Set("passWord", true, "密码").AddRegex("[0-9].*", "用户名必须以数字开头").AddRegex(".{5,15}", "长度为5-15字符").ToOptionItem() ); //register ValidationSugar.Init(PageKeys.REGISTER_KEY, ValidationSugar.CreateOptionItem().Set("userName", true, "用户名").AddRegex("[a-z,A-Z].*", "用户名必须以字母开头").AddRegex(".{5,15}", "长度为5-15字符").AddFunc("checkUserName", "用户名已存在!").ToOptionItem(), ValidationSugar.CreateOptionItem().Set("password", true, "密码").AddRegex(".{5,15}", "长度为5-15字符").ToOptionItem(), ValidationSugar.CreateOptionItem().Set("password2", true, "密码").AddRegex(".{5,15}", "长度为5-15字符").AddFunc("confirmPassword", "密码不一致").ToOptionItem(), ValidationSugar.CreateOptionItem().Set("sex", true, "性别").AddRegex("0|1", "值不正确").ToOptionItem(), ValidationSugar.CreateOptionItem().Set("email", true, "邮箱").Add(ValidationSugar.OptionItemType.Mail, "邮箱格式不正确").ToOptionItem(), ValidationSugar.CreateOptionItem().Set("mobile", false, "手机").Add(ValidationSugar.OptionItemType.Mobile, "手机格式不正确").ToOptionItem(), ValidationSugar.CreateOptionItem().Set("qq", false, "qq").AddRegex(@"/d{4,15}", "qq号码格式不正确").ToOptionItem(), ValidationSugar.CreateOptionItem().Set("education", true, "学历", true/*checkbox 多选模式*/).AddRegex(@"/d{1,15}", "值不正确").ToOptionItem() ); } }}
Global.cs注册我们就可以用了
验证大多情况下分两种
1、submit提交的写法
Register 一行代码搞定、获取绑定信息交给viewbag
PostRegister 也是一行完成后台验证
view
1、引用js并写好初始化函数
2、将@Html.Raw(ViewBag.validationBind) 放在页面最下方
VIEW完整代码:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 | @{
ViewBag.Title = "Register" ;
Layout = null ; } <html xmlns= "http://www.w3.org/1999/xhtml" > <head>
<meta http-equiv= "Content-Type" content= "text/html; charset=utf-8" />
<script src= "/Content/jquery-validation-1.13.1/lib/jquery-1.9.1.js" type= "text/Javascript" ></script>
<script src= "/Content/jquery-validation-1.13.1/dist/jquery.validate.js" type= "text/javascript" ></script>
<script src= "/Content/validation.sugar.js" type= "text/javascript" ></script>
<script src= "/Content/jquery-validation-1.13.1/lib/jquery.form.js" type= "text/javascript" ></script>
<link href= "/Content/jquery-validation-1.13.1/validation.sugar.CSS" rel= "stylesheet"
type= "text/css" />
<script type= "text/javascript" >
$(function () {
var factory = new validateFactory($( "form" ), "<img src=/"/Content/jquery-validation-1.13.1/error.png/" />" );
factory.init();
});
//用户名是否已存在
function checkUserName() {
//实际开发换成: Ajax async:false
var userName = $( "[name=userName]" ).val();
if (userName == "admin1" || userName == "admin2" ) {
return false ;
}
return true ;
}
|