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.
-
Can you just set the ClientValidationFunction property to use your script?Joel Coehoorn– Joel Coehoorn2009-03-12 21:24:18 +00:00Commented 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.Emilio León– Emilio León2009-03-13 19:11:16 +00:00Commented 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.Emilio León– Emilio León2009-03-13 19:19:07 +00:00Commented Mar 13, 2009 at 19:19
Add a comment
|
3 Answers
<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);
}
2 Comments
peroija
Good mention of
ValidatorValidatekaelle
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.
if (Page_ClientValidate('Validation Group'))
{
//Your valid
}
4 Comments
Emilio León
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.
JoshBerke
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?
Emilio León
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?
JoshBerke
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...
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
Emilio León
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.