zoukankan      html  css  js  c++  java
  • 自定义验证规则

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    namespace mvctest
    {
        public static class MVCHellper
        {
            public static string GetErrors(ModelStateDictionary ms)
            {
                System.Text.StringBuilder sbtext = new System.Text.StringBuilder();
                foreach (var item in ms.Keys)
                {
                    if (ms[item].Errors.Count <= 0)
                    { continue; }
                    sbtext.Append("错误消息为:");
                    foreach (var itemerror in ms[item].Errors)
                    {
                        sbtext.Append(itemerror.ErrorMessage + "<br/>");
                    }
    
                }
                return sbtext.ToString();
    
            }
    
    
             
        }
        /// <summary>
        /// 第一种
        /// </summary>
        public class PhoneAttribute : RegularExpressionAttribute
        {
            public PhoneAttribute() : base(@"^(?:+?86)?1(?:3d{3}|5[^4D]d{2}|8d{3}|7(?:[35678]d{2}|4(?:0d|1[0-2]|9d))|9[189]d{2}|66d{2})d{6}$")
            {
                this.ErrorMessage = "手机号不正确";
            }
        }
    
        /// <summary>
        /// 第二种
        /// </summary>
        public class chinaPhoneAttribute : ValidationAttribute
        {
            public chinaPhoneAttribute()
            {
                this.ErrorMessage="错误";
            }
            public override bool IsValid(object value)
            {
                if (value is string)
                {
                    string s = (string)value;
                    if (s.StartsWith("13"))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
                //return base.IsValid(value);
            }
        }
    
             
        
    }
  • 相关阅读:
    静态包含与动态包含
    REST风格下如何放行静态资源
    java迭代器
    es6之扩展运算符 三个点(...)
    关于Echarts配置项的工作记录
    vscode中vue代码格式化的相关配置
    v-loading
    git中Please enter a commit message to explain why this merge is necessary.
    slot
    slot的使用方法
  • 原文地址:https://www.cnblogs.com/lierjie/p/11885814.html
Copyright © 2011-2022 走看看