zoukankan      html  css  js  c++  java
  • 常用验证方法

     /// <summary>         /// 验证正整数         /// </summary>         /// <param name="str"></param>         /// <returns></returns>         public static string zhengshu(String str)         {             string flag = string.Empty;

                Regex r = new Regex(@"^d+$");             if (r.IsMatch(str))             {                 flag = "OK";             }             else             {                 flag = "只能输入0和正整数!";             }

                return flag;         }

            /// <summary>         /// 验证小数         /// </summary>         /// <param name="str"></param>         /// <returns></returns>         public static string xiaoshu(String str)         {             string flag = string.Empty;

                Regex r = new Regex(@"^0$|^(-)?((0)?.d+)$|^(-)?([1-9]d*(.d+)?)$");             if (r.IsMatch(str))             {                 flag = "OK";             }             else             {                 flag = "只能整数和小数!";             }

                return flag;         }

            #region 截取固定长度的字符串         /// <summary>         /// 处理字符串长度 如果长度大于输入长度截取         /// </summary>         /// <param name="info"></param>         /// <param name="length"></param>         /// <returns></returns>         public static string getSafeStr2(string info, int length = 15)         {             if (info == string.Empty)                 return "";

                string retInfo = info.Replace("'", "''");

                if (info.Length > length)             {                 retInfo = info.Substring(0, length)+"...";             }             return retInfo;         }         #endregion

            /// <summary>         /// 验证日期格式         /// </summary>         /// <param name="str"></param>         /// <returns></returns>         public static string riqi(String str)         {             string flag = string.Empty;

                Regex r = new Regex(@"^[1-2]{1}d{3}[0-1]{1}d{1}[0-3]{1}d{1}$");             if (r.IsMatch(str))             {                 flag = "OK";             }             else             {                 flag = "日期格式有误(yyyymmdd)!";             }

                return flag;         }

            /// <summary>         /// 邮箱格式         /// </summary>         /// <param name="str"></param>         /// <returns></returns>         public static string EmailRegex(String str)         {             string flag = string.Empty;

                Regex r = new Regex(@"^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+)+$");             if (r.IsMatch(str))             {                 flag = "OK";             }             else             {                 flag = "邮箱格式有误(yyyymmdd)!";             }

                return flag;         }

            /// <summary>         /// 电话验证         /// </summary>         /// <param name="str"></param>         /// <returns></returns>         public static string PhoneRegex(String str)         {             string flag = string.Empty;

                Regex r = new Regex(@"^((d{11})|^((d{7,8})|(d{4}|d{3})-(d{7,8})|(d{4}|d{3})-(d{7,8})-(d{4}|d{3}|d{2}|d{1})|(d{7,8})-(d{4}|d{3}|d{2}|d{1}))$)");             if (r.IsMatch(str))             {                 flag = "OK";             }             else             {                 flag = "电话格式有误(yyyymmdd)!";             }

                return flag;         }

            /// <summary>         /// 四位数字         /// </summary>         /// <param name="str"></param>         /// <returns></returns>         public static string SZRegex(String str)         {             string flag = string.Empty;

                Regex r = new Regex(@"^d{4}$");             if (r.IsMatch(str))             {                 flag = "OK";             }             else             {                 flag = "数字格式有误(yyyymmdd)!";             }

                return flag;         }

  • 相关阅读:
    IB(InterBase Server) 的完整连接格式
    jna
    编写基于Prototype的Javascript动画类
    Go——使用 go mod ——有依赖的Go工程
    pkgconfig—— PKG_CONFIG_PATH——Makefile——pkgconfig的作用与使用
    Go——Goland Debug报错Version of Delve is too old for this version of Go
    NATS——NATS Streaming 是什么 (转)
    Go——Go语言调用C语言
    go get安装包超时处理
    NATS—基础介绍 (转自 https://www.cnblogs.com/yorkyang/p/8392172.html)
  • 原文地址:https://www.cnblogs.com/conghua/p/3443840.html
Copyright © 2011-2022 走看看