I have a CustomValidator that is validating a telephone number for several different telephone numbering schemes. The client-side javascript looks like this:
validatePhoneNumber(sender, args) {
cleanNumber = args.Value.replace(/\D/, "");
country = $("#" + CountryID).get(0).value;
switch (country) {
case "North America":
args.IsValid = validateNAPhoneNumber(cleanNumber);
if (!args.IsValid) sender.errormessage = "* Not a NA Phone #";
break;
case "UK":
args.IsValid = validateUKPhoneNumber(cleanumber);
if (!args.IsValid) sender.errormessage = "* Not a UK Phone #";
break;
...
}
}
The actual validation takes place properly, and the CustomValidator has the correct IsValid property at all times. The sender.errormessage, however, seems to be rewritten just after this function call to it's default value. How can I change the errormessage value, and make it "stick"?
In order for the Validator 's error message to display in the ValidationSummary , you need to set the Validator s Display="none" . I also set Text="" .
The CustomValidator control is a separate control from the input control it validates, which allows you to control where the validation message is displayed. Validation controls always perform validation on the server.
The CustomValidator control allows writing application specific custom validation routines for both the client side and the server side validation.
The ServerValidate event is raised when validation is performed on the server. This event is used to provide a custom validation routine for an input control, such as a TextBox control.
function dateofbirth(sender, args) {
var dtcDOB = document.getElementById('<%= dtcDOB.ClientID %>');
var dob = new Date(dtcDOB.value);
var currDate = new Date();
if (dtcDOB.value == "") {
args.IsValid = false;
sender.textContent = "Provide DOB.";
return;
}
args.IsValid = true;
}
Try sender.textContent = "your err msg"
.
It worked for me.
The best way to change the error message of a validator control with image is:
sender.innerHTML = "YOUR HTML WITH ANYTHING COME HERE"
To change the error message, do it like this:
if (!args.IsValid) document.getElementById('<%= cstPhoneNumber.ClientID %>').errormessage = "* Not a NA Phone #";
To change the text, do this:
if (!args.IsValid) document.getElementById('<%= cstPhoneNumber.ClientID %>').innerHTML = "* Not a NA Phone #";
cstPhoneNumber
should be replaced by the name of your validation control.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With