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.

2
  • 1
    Why is important to check only one Validator ? You can run validation for whole page, look at stackoverflow.com/questions/1066857/… Commented May 16, 2012 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. Commented May 16, 2012 at 21:16

3 Answers 3

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;
}
Sign up to request clarification or add additional context in comments.

Comments

3

thx sennett (voted)

i just ran the simplest JS

Page_ClientValidate();

if you have a validation group then is

Page_ClientValidate("validationGroupName")

Comments

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.

1 Comment

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".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.