zoukankan      html  css  js  c++  java
  • Mvc_model实体数据验证

    MVC提供了很方便的数据验证,只需要在model里加入相关的正则等,那么就会在前台里生成相关的验证脚本。需要引用两个js文件:

    jquery.validate.min.js

    jquery.validate.unobtrusive.min.js

    [Display(Name = "转入金额")]
            [Required(AllowEmptyStrings = false, ErrorMessage = "请输入转账金额")]
            [Range(100, 20000, ErrorMessage = "输入的金额只能在100元到2万元之间")]
            [RegularExpression(@"^[1-9]d*$", ErrorMessage = "转入的金额必须为整数")]
            public decimal Amount { get; set; }
            /// <summary>
            /// 交易密码
            /// </summary>
            [Display(Name = "交易密码")]
            [StringLength(20, MinimumLength = 6, ErrorMessage = "{0}的长度在{2}至{1}个字符间")]
            [Required(AllowEmptyStrings = false, ErrorMessage = "请输入交易密码")]
            [DataType(DataType.Password)]
            public string TradingPassword { get; set; }
    
        [StringLength(10, MinimumLength = 4, ErrorMessage = "{0}的长度必须大于{2}个字符并小于{1}个字符")]
        [Display(Name = "密码")]
        public virtual string LogPassword { get; set; }
    
        [StringLength(10, ErrorMessage = "{0}的长度不能大于{1}个字符")]
        [Display(Name = "姓名")]
        public virtual string RealName { get; set; }
    
        [Display(Name = "出生日期")]
        //[Range(typeof(DateTime), "2011-12-31", "1950-1-1", ErrorMessage = "{0}的范围是{1}到{2}")]
        public virtual DateTime Birthday { get; set; }
    
        [RegularExpression(@"d{17}[d|X]|d{15}", ErrorMessage = "{0}的格式不正确")]
        [Display(Name = "身份证号码")]
        public virtual string IdentityNo { get; set; }
    
        [RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}", ErrorMessage = "{0}的格式不正确")]
        [Display(Name = "邮箱")]
        public virtual string Email { get; set; }
    
            [Required(ErrorMessage = "注册人邮箱不能为空")]
            [RegularExpression(@"^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$", ErrorMessage = "邮箱不正确")]
            public string Email { get; set; }
  • 相关阅读:
    学生免费注册Pycharm
    CSS笔记
    加载CIFAR数据集时报错的大坑
    发布小程序
    微信中的动图如果发朋友圈
    安卓第一个小项目
    转换小写字母
    1小时搞定vuepress快速制作vue文档/博客+免费部署预览
    干货满满!如何优雅简洁地实现时钟翻牌器(支持JS/Vue/React)
    JavaScript 加减危机——为什么会出现这样的结果?
  • 原文地址:https://www.cnblogs.com/ingstyle/p/6655922.html
Copyright © 2011-2022 走看看