MVC3 RegularExpression is very slow

I try to validate the FileName using regular expression but it is very slow and browser hanging.

If I type the text in the input fast or entiring more than 40 chars then browser hanging. I need to kill it

Here is my model and view.

 public class MyTester
    {
        [DisplayName("Zip File Name")]
        [StringLength(100)]
        [RegularExpression(@"^((\w[\w].*))+(.zip|.ZIP)$", ErrorMessage = "File Name of Zip Archive must terminate in .zip")]
        public string FileName { get; set; }
    }
@model Models.MyTester
@{
    ViewBag.Title = "Home Page";
}

<h2>@ViewBag.Message</h2>

@using (Html.BeginForm())
{

            <div class="row" id="FileNameDiv">
               <div class="control-label">
                   @Html.LabelFor(model => model.FileName)
               </div>
               <div class="controls">
                   @Html.EditorFor(model => model.FileName)
                   @Html.ValidationMessageFor(model => model.FileName)
               </div>
           </div>
           <input type=submit value="Save" />
}

  This has nothing to do with MVC. The behaviour you are seeing because, Jquery Validation 1.10 for regex is eager due to an issue. What happening here is that whenever you press a key regex runs and regex have always not good performace. Just add these line to make your validation only run when you submit the form, Check this blog for further details

$(function() {
    var settngs = $.data($('form')[0], 'validator').settings;
    settngs.onkeyup = false;
    settngs.onfocusout = false;
});

 

posted @ 2013-01-07 15:06  北美上映  阅读(388)  评论(0编辑  收藏  举报