C#写法: public class InputValidation { /// <summary> /// 正则验证的 /// </summary> /// <param name="regExValue">正则表达式</param> /// <param name="itemValue">被验证的数据</param> /// <returns></returns> private static bool IsRegEx(string regExValue, string itemValue) { try { Regex regex = new System.Text.RegularExpressions.Regex(regExValue); if (regex.IsMatch(itemValue)) return true; else return false; } catch (Exception) { return false; } } /// <summary> /// 是否是数字 /// </summary> /// <param name="itemValue"></param> /// <returns></returns> public static bool IsNumeric(string itemValue) { return (IsRegEx("^(-?[0-9]*[.]*[0-9]{0,3})$", itemValue)); } /// <summary> /// 是否是正浮点型 /// </summary> /// <param name="itemValue"></param> /// <returns></returns> public static bool IsPositiveDouble(string itemValue) { return (IsRegEx(@"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$", itemValue)); } /// <summary> /// 是否是正整数 /// </summary> /// <param name="itemValue"></param> /// <returns></returns> public static bool IsPositiveInt(string itemValue) { return (IsRegEx("^[0-9]*[1-9][0-9]*$", itemValue)); } /// <summary> /// 是否是电话号码 /// </summary> /// <param name="itemValue"></param> /// <returns></returns> public static bool IsCellPhone(string itemValue) { return (IsRegEx(@"^(\d*|\d{3,}\-\d*)$", itemValue)); } /// <summary> /// 是否是字母字符串,不分大小写 /// </summary> /// <param name="itemValue"></param> /// <returns></returns> public static bool IsAlphabeticString(string itemValue) { return (IsRegEx(@"^[A-Za-z]+$", itemValue)); } /// <summary> /// 是否是有数字和字符串组成 /// </summary> /// <param name="itemValue"></param> /// <returns></returns> public static bool IsNumberOrChar(string itemValue) { return (IsRegEx(@"^[A-Za-z0-9]+$",itemValue)); } /// <summary> /// 是否是中文 /// </summary> /// <param name="itemValue"></param> /// <returns></returns> public static bool IsChineseCharater(string itemValue) { return (IsRegEx(@"^([\u4e00-\u9fa5]+|[a-zA-Z0-9]+)$ ", itemValue)); } } javascript写法: function regMail(str) var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/; return reg.test(str); } function isNum(s) { return !isNaN(s); } function regTel(str)//验证固定电话 { var reg=/^\d{6,12}$/; return reg.test(str); } |
|