Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I rewrite the ErrorMessage for a CustomValidator control on the client?

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

like image 412
Dustman Avatar asked Aug 04 '09 22:08

Dustman


People also ask

How do you show custom validator error message in ValidationSummary?

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

What is custom validator control?

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.

Which of the following validation control supports custom validation?

The CustomValidator control allows writing application specific custom validation routines for both the client side and the server side validation.

Which event is raised when the custom validator performs 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.


3 Answers

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.

like image 81
Nik Avatar answered Sep 21 '22 11:09

Nik


The best way to change the error message of a validator control with image is:

sender.innerHTML = "YOUR HTML WITH ANYTHING COME HERE"
like image 39
Satyavir Yadav Avatar answered Sep 21 '22 11:09

Satyavir Yadav


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.

like image 44
Andy West Avatar answered Sep 18 '22 11:09

Andy West