分享

C#与javascript—正则写法

 迷之雾 2011-05-11
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);
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多