ASP.NET CustomValidator - Changing error message at client side

The ASP.NET CustomValidator server control is very useful because it allows me to write my own validation logic with a few tweak to the settings and a few lines of codes. Not only it will provide server side validation, but it's able to do client-side validation as well. Furhter information about this can be found on MSDN website.

The only problem that I have, is that I was not able to change the error message based on validation rules that I set at the client-side validation script. I tried to search on the Internet, and all I came across are either setting the source.errormessage, or the source.innerText attributes. Yet, none of them work for me, and I can't even find anything on MSDN that mentioned about this.

So, applying some 'investigating' mind here, I tried to check what will CustomValidator be represented with, and it's represented by a <span> tag. Then, I tried to see if there's any value that I could retrieve in the client-side script by writing the following in the validation function.

alert('sender.id:' + sender.id + ', errormessage:' + sender.errormessage);
alert('sender.id:' + sender.id + ', innertext:' + sender.innerText);

This is how I'd debug the problem. The first alert would show the ID of the control that trigger the validation function, and the sender.errormessage would have the value that I specified. The second alert would show the ID as well, but the sender.innerText would be stated as undefined.

This is interesting to me because if errormessage is present, it means that if I updated the errormessage value, I should be able to get the new message to be displayed, but no. Then, the innerText, which returned undefined, is a bit suspicious here because in the forum.asp.net, this is the solution that provided. After some reading, errormessage will be displayed should there be a validation summary included, so that's definitely out in my case. So what I did, is to search on the net to see if there's a way that I could change the inner text of the <span> tag, and I found this - innerHTML.

Again, to make sure that innerHTML is available to the unit, I tried to insert another alert to display if sender.innerHTML will return something. Yes, it does return the exact value similar to the sender.errormessage. So this time, I tried to set different error message to different validation rules in the client-side validation script to the sender.innerHTML, and it works accordingly. This is the skeleton of the code:

if()
{
sender.innerHTML = ;
client_arguments.IsValid = false;
}
else
{
client_arguments.IsValud = true;
}

Now I could really rest for a while before cracking my brain for another search for solution, sigh...

Comments

Popular posts from this blog

Yahoo! Messenger voice call & video call disabled, why?

Sport's Injuries - Treatment for bruises

Web Development: Disable, Validate, & Enable Buttons