zoukankan      html  css  js  c++  java
  • (3)验证用户的输入

    场景 你要确保你的表单捕获的数据包含你预期的数据,这些数据是基于你的数据库或模型设计。

    解决方案

    .NET 4.0 包含了一个新的数据注解命名空间,提供了一些有用的元数据属性类。这些类已经被应用到MVC3。

    对于验证表单输入,下面的属性类可以用来提供各种各样 验证选项:RequiredAttribute,RegularExpressionAttribute,RangeAttribute和 DataTypeAttribute。当需要自定义的验证的时候,MVC的3还支持改进ValidationAttribute类,允许开发人员定义的验证。

    讨论

    接下来的例子是要去扩展“code-first book“model,这个model是在前一“秘方”中创建的。

    这个model将按照以下条件被更新:

    1. 书名是必须的

    2. ISBN是合法的

    3. 书的摘要是必须的

    4. 作者是必须的

    5. 合法的价格(美元)

    6. 合法的出版日期

    以上6个验证中的5个可以由MVC 3 的内置方法完成。然而,第5个验证需要用一种不同的格式化-它需要一个自定义验证方法。

    双击代码全选
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    public class Book
       {
           public int ID { get; set; }
           [Required]
           public string Title { get; set; }
           [Required]
           [IsbnValidation]
           public string Isbn { get; set; }
           [Required]
           public string Summary { get; set; }
           [Required]
           public string Author { get; set; }
           public string Thumbnail { get; set; }
           [Range(1, 100)]
           public double Price { get; set; }
           [DataType(DataType.Date)]
           [Required]
           public DateTime Published { get; set; }
       }
       public class BookDBContext : DbContext
       {
           public DbSet<Book> Books { get; set; }
       }
     

    在上边的例子,[Required]数据注解被附加在每个字段上,表明这个字段是必须由用户提供。在ISBN number上 [IsbnValidation]特性也被添加了,这是通知MVC 3 IsbnValidation 必须调用IsValid操作,这个操作即将被创建.为了验证价格,[Range] 注解被应用。对于价格的验证,我们也可以用正则表达式特性 [RegularExpression] 来完成。

    如下:

    [RegularExpression (@"(b[d.]*)")]

    public double Price { get; set; }

    最后,对于published date(出版日期)的验证,DataType特性告诉MVC这个字段的类型是一个日期类型。

    一个合法ISBN的定义是:10-13个字符。为何合理的组织代码,自定义验证类将被放在一个单独的文件夹里。

    右键点击项目:添加->新建文件夹。我们为这个文件夹命名为:Validations.在该文件夹点击右键。添加类:IsbnValidationAttribute.cs

    代码如下:

    双击代码全选
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Text.RegularExpressions;
     
    namespace MvcApplication.Validations
    {
        [AttributeUsage(AttributeTargets.Field |
     AttributeTargets.Property, AllowMultiple = false,
     Inherited = true)]
        public class IsbnValidationAttribute :
    System.ComponentModel.DataAnnotations.ValidationAttribute
        {
            /**
            * This class is courtesy:
            * Inversion-of-Control-Dependency-Injection/Spring.net/
            * Spring/Validation/Validators/ISBNValidator.cs.htm
            *
            * This class is used for demonstration purposes
            * of performing an ISBN validation. Should you
            * wish to use this in your project, please
            * consult the license agreement here:
            **/
            private static readonly String SEP = "(?:-|s)";
            private static readonly String GROUP = "(d{1,5})";
            private static readonly String PUBLISHER = "(d{1,7})";
            private static readonly String TITLE = "(d{1,6})";
            static readonly String ISBN10_PATTERN =
            "^(?:(d{9}[0-9X])|(?:" + GROUP + SEP + PUBLISHER +
            SEP + TITLE + SEP + "([0-9X])))$";
            static readonly String ISBN13_PATTERN =
            "^(978|979)(?:(d{10})|(?:" + SEP + GROUP + SEP +
            PUBLISHER + SEP + TITLE + SEP + "([0-9])))$";
            public IsbnValidationAttribute() :
                base("Invalid ISBN number")
            {
            }
            public override bool IsValid(object value)
            {
                // Convert to string and fix up the ISBN
                string isbn = value.ToString();
                string code = (isbn == null)
                ? null :
                isbn.Trim().Replace("-", "").Replace("", "");
                // check the length
                if ((code == null) || (code.Length < 10
                || code.Length > 13))
                {
                    return false;
                }
                // validate/reformat using regular expression
                Match match;
                String pattern;
                if (code.Length == 10)
                {
                    pattern = ISBN10_PATTERN;
                }
                else
                {
                    pattern = ISBN13_PATTERN;
                }
                match = Regex.Match(code, pattern);
                return match.Success && match.Index == 0 &&
                match.Length == code.Length;
            }
        }
    }
     

    创建完这个类记得在 book.cs添加命名空间引用:using MvcApplication.Validations;

    上边的例子包含了一个标准的ISBN验证。这个验证是来自CSharp Open Source example。如果ISBN符合2个正则表达式中的一个。验证函数将返回true。否则返回false。需要用户重新输入

    如果你在你的浏览器里转到图书创建页面。当你点击提交按钮。验证就被会触发。

    年轻不是你玩的理由,而是你奋斗的资本
  • 相关阅读:
    算法导论之贪心算法
    编程过程中遇到的一些细节
    c++11和c99
    面试总结(YY一面)
    python(17):字典
    python(16):元组
    python(15):练习题
    python(14):列表
    python(13):字符串
    python(12):练习题
  • 原文地址:https://www.cnblogs.com/lyaxx1314/p/3385950.html
Copyright © 2011-2022 走看看