5

I want to code a function in javascript that every time I call it in the webform it fires up an specific validator in that same webform.

3
  • Can you just set the ClientValidationFunction property to use your script? Commented Mar 12, 2009 at 21:24
  • Yes indeed I'm doing that with every validator I coded on the webform. But imagine that you need that on certain event (OnChange, OnBlur, etc.) you need to trigger againg the same validator. I want to do this to avoid inserting a new validator that repeats in terms of functionality the other one. Commented Mar 13, 2009 at 19:11
  • What Im trying to do is this: The sum of X number of fields must be equal to 100%. The validator that has this function and the message is always located on the first field (x1) and I'm activating that same validator from the rest of the fields in order to update the message when the sum is 100. Commented Mar 13, 2009 at 19:19

3 Answers 3

18
<asp:RequiredFieldValidator ID="passwordRequiredFieldValidator" runat="server" 
    ErrorMessage="* Password Required!"
    EnableClientScript="true" CssClass="errorText" 
    ControlToValidate="passwordTextBox"> 
</asp:RequiredFieldValidator>

To fire this validator use:

window.ValidatorValidate(window.passwordRequiredFieldValidator);

Further example:

function ValidatePassword() {
    window.ValidatorValidate(window.passwordRequiredFieldValidator);
    var valid = window.passwordRequiredFieldValidator.isvalid;
    alert(valid);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Good mention of ValidatorValidate
I am fairly certain that window.ValidatorValidate(window.passwordRequiredFieldValidator) would result in an error because asp controls append things to the id that appears in the resulting markup. window.ValidatorValidate(window.<%= passwordRequiredFieldValidator.ClientID %>) will get you where you want to go as long an the call is being made on the same page that the control on.
7
if (Page_ClientValidate('Validation Group'))
{
    //Your valid
}

4 Comments

It works flawlesly Josh because I'm not currently using ValidationGroup to group my validators. But I'll keep testing it because in order to make it work I'm using an unique validator group for the validator I want to fire. In the future as the webform gets more complex this won't be possible.
So you want to be able to fire one specific validator? Not rely on the group mechanism? This way you can define your groups dynamically at runtime?
You're right againg Josh, I didn't think of defining groups dinamically, thanks for this info. It's a different perspective from what I was thinking but is certainly right and clean?
Sure...you can also fire up Fiddler and walk through the javascript emitted for the validators...I think you might even be able to call a valid() method on the control to determine if its valid...
0

The "CustomValidator" control lets you use javascript to validate your form. If you do this you should also do the same validation on the server so that the user can't just disable javascript to bypass the validation errors.

1 Comment

Good Observation David. I once did it, but in this case I'm turning validators on and off dynamically via JavaScript according to webform conditions, so in order to validate in the server also, I have to let know the server which validators where turned on and off too.

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.