0

My question isn't ASP.NET Remote Validation only on blur? because I asked to do with model properties.

I'm using a a model with properties, which are reflected in the client's web browser, and then, when I press the submit button, the ErrorMessages are printed in the web browser. I want that when the client leave the input focused, no when I press the submit button, the server process the request (only the input "disfocused") and, in error case, show the ErrorMessage in the web browser.

My register model:

    [DataType(DataType.EmailAddress)]
    [Required(ErrorMessage = "El field {0} is obligatory.")]
    [Display(Name = "Email")]
    [StringLength(80, ErrorMessage = "Email too large.")]
    [RegularExpression(@"^([a-zA-Z0-9._-]+)@(outlook|hotmail|yahoo)\.\w{2,}$", ErrorMessage = "Invalid email")]
    public string Email { get; set; }
Community
  • 1
  • 1
Joe
  • 307
  • 5
  • 14
  • possible duplicate of [ASP.NET Remote Validation only on blur?](http://stackoverflow.com/questions/5407652/asp-net-remote-validation-only-on-blur) – CodeCaster May 04 '15 at 10:44

1 Answers1

0

This can been done on the client side with

Layout page needs

@Render.Section("Scripts",required:false)

Your view would then have

@section Scripts {
   @Scripts.Render("~/bundles/jqueryval")
}

Your Bundle would be . This Bundle is added by default in a standard ASP MVC project in visual studio

 bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                    "~/Scripts/jquery.validate*"
                    ));

The view would then just need a @Html.ValidationMessageFor for each property

see more info here ASP MVC validation

  • This run, but no when I "disfocus" the input. This run when I write something in the input. – Joe May 04 '15 at 11:55
  • @Joe sorry i dont quite understand what you seem to be saying –  May 04 '15 at 11:57
  • Well, I want when the client write in the "username" input, don't occur nothing, but when I have no the focus in this input (the website, other input, submit button...), the validation will be done and printed, no before. – Joe May 04 '15 at 12:01
  • @Joe but then why do you say that "ASP.NET Remote Validation only on blur" wont work? Because that should work. Although you are using model properties , the properties are still acting on jquery validation –  May 04 '15 at 12:06
  • Sorry, I don't understand you. I'm saying your answer runs and validate the input, but WHEN I write one character (a lot of validations per input), no when I focus another element. – Joe May 04 '15 at 12:13
  • For example, when you want register an account in Outlook (https://signup.live.com/signup), the validations are done when I press submit or I leave the actual input, no when I'm writting. – Joe May 04 '15 at 12:20
  • 1
    which is why you should see this link for your answer http://stackoverflow.com/questions/5407652/asp-net-remote-validation-only-on-blur –  May 04 '15 at 12:31
  • Iv'e added in the layout "". However, the solution apparently runs well, but if you return to the writed input, the behaviour is onkeyup:true. – Joe May 04 '15 at 12:46
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/76894/discussion-between-gerdi-and-joe). –  May 04 '15 at 13:34