2

I have an ajax form on razor view engine. For validation i use dataanotation classes. Validation work fine when user submit the form, validation messages work fine. The problem is, validation wont work on keyup or blur events.

How can i activate validation without submit on ajaxform(ajax.beginform)

Here is my view code:

@using (Ajax.BeginForm(new AjaxOptions { InsertionMode = InsertionMode.Replace,       
        UpdateTargetId = "employeeDetail", HttpMethod = "Post", OnComplete = "Complete", 
        Confirm = "Confirm?" }))
{
    @Html.TextBoxFor(model => model.Email)
    @Html.ValidationMessageFor(model=>model.Email)
    <span style="float:right"><input type="submit" class="tableGenelButton" id="submitButton" value="Kaydet" /></span>
}

Model:

  [RequiredWithMessage]
  [Display(Name = "E-Mail")]
  public string Email { get; set; }
spinodal
  • 4,007
  • 3
  • 29
  • 38
Yorgo
  • 2,668
  • 1
  • 16
  • 24

2 Answers2

3

Update:

Ok, apparently you're using Ajax.BeginForm which uses MicrosoftAjax in stead of jQuery (I didn't realize that before). This one needs some extra work to enable client side validation:

You need

<% Html.EnableClientValidation(); %> 

Somewhere in your page, and also links to MicrosoftAjax.js, MicrosoftMvcAjax.js and MicrosoftMvcValidation.js

Here's a link that might be interesting for you:

ASP.NET MVC Client Side Validation With Ajax.BeginForm


To enable client side validation for a custom validation attribute (I guess [RequiredWithMessage] is one of those), you have to implement the IClientValidatable interface.

Here is an article that explains how to do that:

The Complete Guide To Validation In ASP.NET MVC 3 - Part 2

fretje
  • 8,322
  • 2
  • 49
  • 61
  • The problem is not in custom validation attribute. I also get the same problem when i use default [Required] dataanotation class. – Yorgo May 10 '11 at 07:20
  • @user: [Required] should work out of the box in MVC 3. Make sure you have a setting `` under `appSettings` in your web.config file. And of course you also need to have a reference to jquery and jquery.validate and jquery.validate.unobstrusive (in case `UnobtrusiveJavaScriptEnabled` is true) in your view. – fretje May 10 '11 at 07:25
  • 1
    i did everything you told before. I add all refs. in to layout page also add "ClientValidationEnabled" setting to web.config. – Yorgo May 10 '11 at 10:18
  • Aah, I see now you're using `Ajax.BeginForm`. I thought you were using `Html.BeginForm`. I don't have experience with Ajax.BeginForm and client side validation. I know it should work (out of the box) with Html.BeginForm though. – fretje May 10 '11 at 10:33
  • :D you miss understood me. np thanx – Yorgo May 10 '11 at 11:37
0

Assuming you have client side validation enabled, you will need to call .validate() in the relevant events:

$("#theFormToValidate input").blur(function(){
    $("#theFormToValidate").validate();
});

and

$("#theFormToValidate input").keyup(function(){
    $("#theFormToValidate").validate();
});
Community
  • 1
  • 1
Adam Flanagan
  • 3,052
  • 23
  • 31
  • These should happen automatically... I think the OP is talking about client side validation for his custom validation attribute (`[RequiredWithMessage]`). – fretje May 09 '11 at 16:13