I want to set ASP.net custom validator error parameter text through client side javascript. How can access it via sender, args parameters in my function?
Asked
Active
Viewed 7,991 times
0
-
See also http://stackoverflow.com/questions/1230281/how-can-i-rewrite-the-errormessage-for-a-customvalidator-control-on-the-client – Dexter Oct 25 '10 at 07:20
2 Answers
3
This worked for me:
var clientValidationFunction = function(sender, args) {
sender.textContent = sender.innerText = sender.innerHTML = "My new error text";
// etc...
};
I just looked at the sender object and replaced all occurrences of the current error string, with the new error string.

David Sherret
- 101,669
- 28
- 188
- 178
3
All you need to do is define the callback method in the ClientValidationFunction property of the CustomValidator definition:
<asp:CustomValidator id="CustomValidator1"
...
ClientValidationFunction="ClientValidationFunction" />
You can then define a client side validation script:
<script language="javascript">
function ClientValidationFunction(sender, args){
var valid = false;
// Validation logic..
sender.errormessage = "Validation failed";
args.IsValid = valid;
return;
}
</script>
Update: The sender variable holds a reference to the custom validator control - because JavaScript is dynamically typed, we can just update its errormessage
property directly:
sender.errormessage = "This is a new validation message";

Dexter
- 18,213
- 4
- 44
- 54
-
Thanks Dexter, I want to know how to set the validator's error message parameter throught javascript. – Zo Has Oct 25 '10 at 07:03
-
-