views:

729

answers:

2

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?

A: 

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;
jacko
A: 

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.

Methee