开发者

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

开发者 https://www.devze.com 2022-12-19 05:17 出处:网络
I have a username textbox on a form, that has a few validation rules applied to it via the DataAnnotation attributes:

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开发者_如何学Python(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?


Ok so my solution is to manually trigger client-side validation during the button onclick event:

        var validator =  $("form").validate({
            submitHandler: function(form) { /* do nothing */ }
        });
        if (validator.errorList.length > 0) return;


How about:

$(function() {
    $("#FtpLoginCheck").live("click", function(e){ 
          e.preventDefault();
          var $this = $(this);
          check($this);
    }
});

This will be fired after the asp.net MVC2 do the client validation.

0

精彩评论

暂无评论...
验证码 换一张
取 消