Welcome to AspAdvice Sign in | Join | Help

Change the border color of the controls whose validation fails

Ever wondered how you can change the border color settings of the control whose client side validation fails, so you can catch the user attention easily.. Well I did. I tried with the asp.net validation controls given by the .net framework, but none of those validation controls facilitate this. So, I dug around little deeper and found a way to do that. Ofcourse, that involves modifying the WebUIValidation.js file provided with asp.net.

For folks who are wondering what  this WebUIValidation.js file does. This is a javascript file provided by microsoft with all the javascript code used by the various asp.net validation controls. This file is found in the "aspnet_client\system_web\1_1_4322" folder under your wwwroot folder

Ok. Lets get to the point here.

Ohh.. by the way, I am assuming we all use ValidationSummary control in our pages to summarise all the validation error messages at one place, if not this may not be much helpful to you, although this trick may give you some clues

Before we begin place the following javascript function somewhere in the WebUIValidation.js file

function changeBorder(id)

{

var control = document.getElementById(id);

control.style.borderWidth = "1px";

control.style.borderStyle = "solid";

control.style.borderColor="red";

}

Now....

Look for the function named ValidationSummaryOnSubmit in the WebUIValidation.js file. This method is used by the validation summary control to iterate through  all the invalid controls and output their error messages in certain format

If you look inside the method, you notice that there is a conditional block, which checks to see if the Page is valid..

if (!Page_IsValid) {

......

}

 

In this If loop, there is a "for" loop, which iterates through all the invalid controls..

for (i=0; i<Page_Validators.length; i++) {

if (!Page_Validators[i].isvalid && typeof(Page_Validators[i].errormessage) == "string") {

s += pre + Page_Validators[i].errormessage + post;

}

}

In this loop put the following code

changeBorder(Page_Validators[i].controltovalidate);

Now the above for loop would look something like this

for (i=0; i<Page_Validators.length; i++) {

if (!Page_Validators[i].isvalid && typeof(Page_Validators[i].errormessage) == "string") {

/*Your  Code -- Not Microsoft's*/

changeBorder(Page_Validators[i].controltovalidate);

/*End of Your Code*/

s += pre + Page_Validators[i].errormessage + post;

}

}

 

Thats all.. All of your invalid controls will have a thin red border color when they are invalid

 

Sponsor
Published Tuesday, September 27, 2005 3:21 AM by kreddi

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Change the border color of the controls whose validation fails

Excellent post.
Tuesday, January 10, 2006 10:27 AM by Patrick

# re: Change the border color of the controls whose validation fails

This is really great!

However, I would like to do the same with ASP.NET 2.0. And I learned that it does not use anymore aspnet_client and WebUIValidation.js...

Do you know how I would achieve the same with ASP.NET 2.0?

(related post in asp.net newsgroup here : http://groups.google.ca/group/microsoft.public.dotnet.framework.aspnet/browse_thread/thread/7bbb775d387c4ea5/385e89eb504d5fef#385e89eb504d5fef )

Thanks!
Tuesday, January 17, 2006 1:24 PM by ibiza

# re: Change the border color of the controls whose validation fails

An Excellent job keep us informed of such new improvements…
Friday, January 20, 2006 1:29 PM by inknanand

# re: Change the border color of the controls whose validation fails

Heres a simple fudge to recreate similiar functionality in ASP.NET 2.0.

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" style="border:1px solid red;">
<asp:TextBox ID="TextBox1" runat="server" style="visibility:visible;" />
</asp:RequiredFieldValidator>

You lose the error messages and it doesnt quite display correctly in firefox, along with a whole heap of other stuff wrong with it ... but  ... if you only want basic functionality it might suit your needs.

Great post by the way, if only someone had a decent solution for ASP.NET 2.0
Tuesday, May 16, 2006 7:03 PM by Duncan

# re: Change the border color of the controls whose validation fails

In my case when I correct the wrong field the border doesn't change to original border color. How to solve this problem?
Thursday, June 08, 2006 2:20 PM by Fernando Ventura Jr.(fventura@furnas.com.br)

# re: Change the border color of the controls whose validation fails

Can anyone with a ASPNET 2.0 Wep app paste the HTML code in here (Hope it has validators and some buttons with the CauseValidation property set to true) so we could search a way to replace the old (1.x) .js files?

Tuesday, October 03, 2006 1:15 PM by Jportelas

# re: Change the border color of the controls whose validation fails

utytu

Wednesday, February 07, 2007 8:12 AM by ytuytu

# re: Change the border color of the controls whose validation fails

dfds

Tuesday, September 18, 2007 2:21 PM by sd

# re: Change the border color of the controls whose validation fails

fdg

Friday, January 11, 2008 3:11 AM by dsfsdf

# re: Change the border color of the controls whose validation fails

rerrrgh

Friday, January 25, 2008 4:39 AM by fsdghedf

# re: Change the border color of the controls whose validation fails

You can make a customvalidator and put the script above in the clientvalidation function. This way you do not have to modify the aspnet js file.

Thursday, May 15, 2008 7:08 AM by Corne

Leave a Comment

(required) 
required 
(required) 
Enter the code you see below