如果平时不习惯写javascript的情况下,使用jquery validator写起来就是比较头痛的事情,为了解决这一问题所以写了个针对jquery validator 的c# helper。使用helper后编写验证就能得到VS.NET的支持主要是提供感知功能方便编写。
<%=Script.DotNet.JQueryHelper.Validate("signupForm") .Add("firstname", i => { i.required("Please enter your firstname"); }) .Add("lastname", i => { i.required("Please enter your lastname"); }) .Add("username", i => { i.required("Please enter a username").minlength(2, "Your username must consist of at least 2 characters"); }) .Add("password", i => { i.required("Please provide a password").minlength(5,"Your password must be at least 5 characters long"); }) .Add("confirm_password", i => { i.required("Please provide a password").minlength(5, "Your password must be at least 5 characters long").equalTo("Please enter the same password as above"); }) .Add("email", i => { i.required().email(); }) .Add("topic", i => { i.required("#newsletter:checked", null).minlength(2); }) .Add("agree", i => { i.required(); }) %>
如果用javascript描述如下:
从代码上来看是不是helper会简洁很多:) helper完整代码如下:
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Text;namespace Script.DotNet{ public class JQueryHelper { public static Validate Validate(string name) { return new Validate(name); } } public class Validate { private string mName; public Validate(string id) { mName = id; } private ListmItems = new List (8); public Validate Add(string name, Action addaction) { ValidateItem item = new ValidateItem(name); mItems.Add(item); addaction(item); return this; } public override string ToString() { StringBuilder sb = new StringBuilder(); ValidateItem item; sb.Append(" \r\n"); return sb.ToString(); } } public class ValidateItem { public ValidateItem(string name) { Name = name; HasMessage = false; } public string Name { get; set; } public ValidateItem required() { return AddType("required", "true", null); } public ValidateItem required(string message) { return AddType("required", "true", message); } public ValidateItem required(string value,string message) { return AddType("required", "'"+value+"'", message); } public ValidateItem remote(string url) { return AddType("remote", url, null); } public ValidateItem remote(string url, string message) { return AddType("remote", url, message); } public ValidateItem remote(string url, string dataexpression, string message) { return AddType("remote", string.Format("{url:'{0}',data:{1}}", url, dataexpression), message); } public ValidateItem email() { return AddType("email", "true", null); } public ValidateItem email(string message) { return AddType("email", "true", message); } public ValidateItem url() { return AddType("url", "true", null); } public ValidateItem url(string message) { return AddType("url", "true", message); } public ValidateItem date() { return AddType("date", "true", null); } public ValidateItem date(string message) { return AddType("date", "true", message); } public ValidateItem dateISO() { return AddType("dateISO", "true", null); } public ValidateItem dateISO(string message) { return AddType("dateISO", "true", message); } public ValidateItem number() { return AddType("number", "true", null); } public ValidateItem number(string message) { return AddType("number", "true", message); } public ValidateItem digits() { return AddType("digits", "true", null); } public ValidateItem digits(string message) { return AddType("digits", "true", message); } public ValidateItem creditcard() { return AddType("creditcard", "true", null); } public ValidateItem creditcard(string message) { return AddType("creditcard", "true", message); } public ValidateItem equalTo(string control) { return AddType("equalTo", "'#" + control + "'", null); } public ValidateItem equalTo(string control, string message) { return AddType("equalTo", "'#"+control+"'", message); } /// /// $("#myform").validate({ /// rules: { /// field: { /// required: true, /// accept: "xls|csv" /// } /// } ///}); /// /// ///public ValidateItem accept(string type) { return AddType("accept", type, null); } /// /// $("#myform").validate({ /// rules: { /// field: { /// required: true, /// accept: "xls|csv" /// } /// } ///}); /// /// /// ///public ValidateItem accept(string type, string message) { return AddType("accept", type, message); } public ValidateItem maxlength(int length) { return AddType("maxlength", length, null); } public ValidateItem maxlength(int length, string message) { return AddType("maxlength", length, message); } public ValidateItem minlength(int length) { return AddType("minlength", length, null); } public ValidateItem minlength(int length, string message) { return AddType("minlength", length, message); } public ValidateItem rangelength(int min,int max) { return AddType("rangelength", string.Format("[{0},{1}]",min,max), null); } public ValidateItem minlength(int min, int max, string message) { return AddType("rangelength", string.Format("[{0},{1}]", min, max), message); } public ValidateItem range(object min, object max) { return AddType("range", string.Format("[{0},{1}]", min, max), null); } public ValidateItem range(object min, object max, string message) { return AddType("range", string.Format("[{0},{1}]", min, max), message); } public ValidateItem max(object value) { return AddType("max", value, null); } public ValidateItem max(object value, string message) { return AddType("max", value, message); } public ValidateItem min(object value) { return AddType("min", value, null); } public ValidateItem min(object value, string message) { return AddType("min", value, message); } public ValidateItem AddType(ValidateType type, object value) { return AddType(Enum.GetName(typeof(ValidateType), type), value, null); } public ValidateItem AddType(ValidateType type, object value, string message) { return AddType(Enum.GetName(typeof(ValidateType), type), value, message); } public bool HasMessage { get; set; } public ValidateItem AddType(string type, object value, string message) { ValidateSubItem item = new ValidateSubItem { Name = type, Message = message, Value = value }; if (!string.IsNullOrEmpty(item.Message)) HasMessage = true; Items.Add(item); return this; } private IList mItems = new List (); public IList Items { get { return mItems; } } public class ValidateSubItem { public string Name; public object Value; public string Message; } } public enum ValidateType { required, remote, email, url, date, dateISO, number, digits, creditcard, equalTo, accept, maxlength, minlength, rangelength, range, max, min }}