5

I want to run whatever client-side validation routine is hooked up to a particular text input element.

The validation has been set up using CustomValidator:

<asp:textbox id="AddEstTime" runat="server" Width="55px"></asp:textbox><br />
<asp:CustomValidator ID="AddEstTimeCustomValidator" ClientValidationFunction="AddEstTimeCustomValidator_ClientValidate" OnServerValidate="AddEstTimeCustomValidator_ServerValidate" ErrorMessage="Please enter a time" ControlToValidate="AddEstTime"  runat="server" Display="Dynamic" ValidateEmptyText="true"/>
<asp:CheckBox ID="AddIsTM" runat="server" Text="T&amp;M" />

and the javascript:

function AddEstTimeCustomValidator_ClientValidate(sender, args) {
    var checkbox = $("input[id$='IsTM']");
    args.IsValid = checkbox.is(":checked") || args.Value.match(/^\d+$/);
}

When the CheckBox "AddIsTM" state changes, I want to revalidate the textbox "AddEstTime", using its hooked-up CustomValidator "AddEstTimeCustomValidator".

I am aware of focus -> add a character refocus -> remove character. I am trying to find a more correct way. New to asp.NET.

sennett
  • 8,014
  • 9
  • 46
  • 69
  • 1
    Why is important to check only one Validator ? You can run validation for whole page, look at http://stackoverflow.com/questions/1066857/determine-if-page-is-valid-in-javascript-asp-net – Antonio Bakula May 16 '12 at 21:04
  • Thanks for the suggestion. It causes all the other validation errors to appear, so errors appear everywhere before the user has had a chance to get to those parts. – sennett May 16 '12 at 21:16

3 Answers3

4

After looking through the Microsoft client-side code, I came up with this which seems to work:

// client-side validation of one user-control.
// pass in jquery object with the validation control
function ValidateOneElement(passedValidator) {
    if (typeof (Page_Validators) == "undefined") {
        return;
    }
    $.each(Page_Validators, function (index, value) {
        if ($(value).attr("id") == passedValidator.attr("id")) {
            ValidatorValidate(value, null, null);
        }
    });
}

This was after examining the Page_ClientValidate function:

function Page_ClientValidate(validationGroup) {
    Page_InvalidControlToBeFocused = null;
    if (typeof(Page_Validators) == "undefined") {
        return true;
    }
    var i;
    for (i = 0; i < Page_Validators.length; i++) {
        ValidatorValidate(Page_Validators[i], validationGroup, null);
    }
    ValidatorUpdateIsValid();
    ValidationSummaryOnSubmit(validationGroup);
    Page_BlockSubmit = !Page_IsValid;
    return Page_IsValid;
}
sennett
  • 8,014
  • 9
  • 46
  • 69
3

thx sennett (voted)

i just ran the simplest JS

Page_ClientValidate();

if you have a validation group then is

Page_ClientValidate("validationGroupName")
bresleveloper
  • 5,940
  • 3
  • 33
  • 47
1

If you want stick with ASP.NET validators eventually you can abuse Validation Groups, but I think that this approach will give you nothing but trouble. Other option is to use jQuery on the client (nice list) only then you will have to duplicate validation on the server side, or to avoid that call server methods from client validations.

Community
  • 1
  • 1
Antonio Bakula
  • 20,445
  • 6
  • 75
  • 102
  • That was exactly what I ended up doing. I guess the answer is "no, there isn't a simple way of running a validator for one control". – sennett May 17 '12 at 00:16