zoukankan      html  css  js  c++  java
  • .Net MVC 前台验证跟后台验证

    前台验证:

    首先你得有一个参数类,参数类代码如下

    验证标记总结

            [DisplayName("邮箱:")]
            [Required(ErrorMessage = "请输入您的邮箱")]
            [RegularExpression(@"^(w-*.*)+@(w-?)+(.w{2,})+$",ErrorMessage ="请输入正确的邮箱")]
            public string email { get; set; }

     然后再去视图里面利用ValidactionMessageFor验证参数类

    <p>
        <label id="label_mail" for="mail">邮箱</label>
        @Html.TextBoxFor(model => model.email, new { @class = "user_input", tabindex = "4" })
        @Html.ValidationMessageFor(model => model.email)

    </p>

    后台验证:个人觉得还是写一个验证类比较合适,

    验证类:

            public  bool Isemail(string isemail) {
                    Regex RegCHZN = new Regex(@"^(w-*.*)+@(w-?)+(.w{2,})+$");
                    Match m = RegCHZN.Match(isemail);
                    return m.Success;
                  }

    在控制器里面调用判断是否验证成功:

     private bool Validate(Field field)
            {

        bool isemail=validate.Isemail(field.email);

        if(isemail==true)

         {

            return true;

         }

        else{

          return false;

          }

       }

                if (ModelState.IsValid)
                {

          //用来判断是否通过前端验证

          if(){//这里用来写后端验证

           }
                }

     纯手打,转载请出示原文链接

  • 相关阅读:
    Linux下链接mysql数据库的命令
    linux cp命令参数及用法详解
    svn命令在linux下的使用
    把一个一维数组转换为in ()
    JS修改标签中的文本且不影响其中标签
    Underscore template
    JavaScript动态加载js文件
    JavaScript库基本格式写法
    JavaScript class 使用
    Uncaught TypeError: Cannot read property 'ownerDocument' of null
  • 原文地址:https://www.cnblogs.com/cjdonet/p/6113031.html
Copyright © 2011-2022 走看看