常用的验证

前端之家收集整理的这篇文章主要介绍了常用的验证前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
手机号码验证,多个手机用,号分割
<script type="text/javascript">
  // var regTel = ^(1[3|4|5|8]\d{9}$/;  //单个手机
  var regTel = /^(1[3|4|5|8]\d{9}\,)*(1[3|4|5|8]\d{9})$/;
    if ($("#txtPhone").val() == "") {
        alert("请填写手机!");
        return;
    }
    if (!$("#txtPhone").val().match(regTel)) {
        alert("请输入正确的手机!");
        return;
    }
</script>

前台页面js中要获取url参数时候
var Request = new QueryString();
function QueryString() {
    var name,value,i;
    var str = location.href;
    var num = str.indexOf("?")
    str = str.substr(num + 1);
    var arrtmp = str.split("&");
    for (i = 0; i < arrtmp.length; i++) {
        num = arrtmp[i].indexOf("=");
        if (num > 0) {
            name = arrtmp[i].substring(0,num);
            value = arrtmp[i].substr(num + 1);
            this[name] = value;

        }
    }
}

引用完后可以在脚本中直接用Request["参数"]


实时显示时间

 <script type="text/javascript">
        $(function () {
            clock();
        });

        //e实时显示时间
        function clock() {
            var d = new Date()
            var year = d.getFullYear();
            var month = d.getMonth() + 1;
            var day = d.getDate();
            var hours = d.getHours();
            var minutes = d.getMinutes();
            var Seconds = d.getSeconds();
            var num = (year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + Seconds).split(' ');
            var num1 = num[0].split('-');
            var num2 = num[1].split(':');
            var d = new Date(num1[0],num1[1],num1[2],num2[0],num2[1],num2[2]);
            var t = d.getTime();
            t += 1000;
            var date = new Date(t);
            var year = date.getFullYear();
            var month = date.getMonth();
            var day = date.getDate();
            var hour = date.getHours();
            var minute = date.getMinutes();
            var second = date.getSeconds();
            var months = new Array("","1","2","3","4","5","6","7","8","9","10","11","12");
            var monthname = months[month];
            if (hour > 24) {
                hour = hour - 12;
            }
            if (monthname < 10)
                monthname = "0" + monthname;
            if (day < 10)
                day = "0" + day;
            if (minute < 10) {
                minute = "0" + minute;
            }
            if (second < 10) {
                second = "0" + second;
            }
            $("#txtTime").val(year + "-" + monthname + "-" + day + " " + hour + ":" + minute + ":" + second);
            setTimeout("clock()",1000);
        }

    </script>

C# 常用验证

    //验证用户名
    public static bool IsName(string input)
    {
        Regex regex = new Regex("^[a-zA-Z][a-zA-Z0-9]{3,19}$");
        return regex.IsMatch(input);

    }
    //验证密码
    public static bool IsWord(string input)
    {
        Regex regex = new Regex("^(?=.*?[0-9])(?=.*?[a-zA-Z])[0-9a-zA-Z]+$");
        return regex.IsMatch(input);
    }
    //验证密码位数
    public static bool IsWord2(string input)
    {
        Regex regex = new Regex("^[a-zA-Z0-9][a-zA-Z0-9]{7,19}$");
        return regex.IsMatch(input);

    }
    //验证电话号码
    public static bool IsPhone(string input)
    {

        Regex regex = new Regex("^0\\d{2,3}-\\d{7,8}$");
        return regex.IsMatch(input);

    }

    //验证手机
    public static bool IsMobilePhone(string input)
    {
        Regex regex = new Regex("^(13|15)\\d{9}$");
        return regex.IsMatch(input);

    }
    //验证email
    public static bool IsEmail(string input)
    {
        Regex regex = new Regex("\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
        return regex.IsMatch(input);

    }
    //验证qq
    public static bool Isqq(string input)
    {
        Regex regex = new Regex("^[1-9][0-9]{4,8}$");
        return regex.IsMatch(input);

    }

    //验证网址
    public static bool Iswww(string input)
    {
        Regex regex = new Regex("http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?");
        return regex.IsMatch(input);

    }
    //验证日期
    public static bool IsDate(string input)
    {
        Regex regex = new Regex("^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$");
        return regex.IsMatch(input);

    }
    //验证hanzi
    public static bool IsHanzi(string input)
    {
        Regex regex = new Regex("^[\u4e00-\u9fa5]+$");
        return regex.IsMatch(input);

    }
    //验证商品数量
    public static bool Isshuliang(string input)
    {
        Regex regex = new Regex("^[1-9][0-9]{0,4}$");
        return regex.IsMatch(input);

    }
    //验证价钱
    public static bool IsMoney(string input)
    {
        Regex regex = new Regex("^\\d+(\\.\\d\\d)?$");
        return regex.IsMatch(input);

    }


    //验证URL
    public static bool IsUrl(string input)
    {
        Regex regex = new Regex("^(http|https|ftp)\\://[a-zA-Z0-9\\-\\.]+\\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\\-\\._\\?\\,\'/\\\\+&%\\$#\\=~])*$"
);
        return regex.IsMatch(input);

    }

  /// 
  /// 判断输入的字符串只包含数字
  /// 可以匹配整数和浮点数
  /// ^-?\d+$|^(-?\d+)(\.\d+)?$
  public static bool IsNumber(string input)
  {
   string pattern = "^-?\\d+$|^(-?\\d+)(\\.\\d+)?$";
   Regex regex = new Regex(pattern);
   return regex.IsMatch(input);
  }

  /// <summary>
  /// 检查是否是数字
  /// true表示是数字,false表示否
  /// </summary>
  /// <param name="str"></param>
  /// <returns></returns>

  public static bool RteNum(string str)
  {
      if (str == "" || str == null)
      {
          return false;
      }
      else
      {
          System.Text.RegularExpressions.Regex reg1 = new System.Text.RegularExpressions.Regex(@"^[-]?\d+[.]?\d*$");

          return reg1.IsMatch(str);
      }

  }

常见的正则帮助类

	/// <summary>
        /// 清除包含'字符串
        /// </summary>
        public const string CLEAN_STRING = @"[']";

        /// <summary>
        /// 验证字符串是否为字符begin-end之间
        /// </summary>
        public const string IS_VALID_BYTE = @"^[A-Za-z0-9]{#0#,#1#}$";

        /// <summary>
        /// 验证字符串是否为年月日
        /// </summary>
        public const string IS_VALID_DATE =
            @"^2\d{3}-(?:0?[1-9]|1[0-2])-(?:0?[1-9]|[1-2]\d|3[0-1])(?:0?[1-9]|1\d|2[0-3]):(?:0?[1-9]|[1-5]\d):(?:0?[1-9]|[1-5]\d)$";

        /// <summary>
        /// 验证字符串是否为小数
        /// </summary>
        public const string IS_VALID_DECIMAL = @"[0].\d{1,2}|[1]";

        /// <summary>
        /// 验证字符串是否为EMAIL
        /// </summary>
        public const string IS_VALID_EMAIL =
            @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";

        /// <summary>
        /// 验证字符串是否为IP
        /// </summary>
        public const string IS_VALID_IP =
            @"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$";

        /// <summary>
        /// 验证字符串是否为后缀名
        /// </summary>
        public const string IS_VALID_POSTFIX = @"\.(?i:{0})$";

        /// <summary>
        /// 验证字符串是否为电话号码
        /// </summary>
        public const string IS_VALID_TEL = @"(\d+-)?(\d{4}-?\d{7}|\d{3}-?\d{8}|^\d{7,8})(-\d+)?";

        /// <summary>
        /// 验证字符串是否为URL
        /// </summary>
        public const string IS_VALID_URL = @"^[a-zA-z]+://(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?$";




原文链接:https://www.f2er.com/regex/361941.html

猜你在找的正则表达式相关文章