ASP.NET MVC2 RC : How to intercept or trigger client-side validation before ajax request?

Posted by jacko on Stack Overflow See other posts from Stack Overflow or by jacko
Published on 2010-02-03T13:54:59Z Indexed on 2010/04/03 6:43 UTC
Read the original article Hit count: 696

Filed under:
|

I have a username textbox on a form, that has a few validation rules applied to it via the DataAnnotation attributes:

[Required(ErrorMessage = "FTP login is required")]
[StringLength(15, ErrorMessage = "Must be 15 characters or fewer")]
[RegularExpression(@"[a-zA-Z0-9]*", ErrorMessage = "Alpha-numeric characters only")]
public string FtpLogin { get; set; }

I also have a button next to this text box, that fires off a jQuery ajax request that checks for the existence of the username as follows:

<button onclick="check(this);return false;" id="FtpLoginCheck" name="FtpLoginCheck">Available?</button>

I'm looking for a way of tieing the two together, so that the client-side validation is performed before the call to the "check(this)" in the onclick event.

Edit: To be more clear, I need a way to inspect or trigger the client-side validation result of the textbox, when I click the unrelated button beside it.

Edit: I now have the button JS checking for $("form").validate().invalid, but not displaying the usual validation messages. Almost there

Any ideas?

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about asp.net-mvc-2-rc