Blue Theme Orange Theme Green Theme Red Theme
 
DevExpress Free UI Controls
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
DevExpress UI Controls
Search :       Advanced Search »
Home » ASP.NET Controls » Use image for error display in ASP.Net validators

Use image for error display in ASP.Net validators

This articles demonstrates the use of images to display errors in ASP.Net for all kinds of validators.

Page Views : 21060
Downloads : 442
Rating :
 Rate it
Level : Intermediate
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
imagevalidator.zip
 
 
DevExpress Free UI Controls
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

One interesting way of showing your error messages when using validation controls is to use images instead of  text for identifying errors on your ASP.NET pages.

Here I will demonstrate you that by using Custom validator. Why Custom validator? Because it provides more flexibility in writing validation rules using javascript.

<script type="text/javascript" language="javascript">

function isInteger(s)

{

 var i; for (i = 0; i < s.length; i++)

 { // Check that current character is number.

 var c = s.charAt(i);

 if (((c < "0") || (c > "9"))) return false;

 } return true;

 // All characters are numbers

 }

 

function validate_Integer(source, args)

 {

 var txtintval=document.getElementById("<%= txtInt.ClientID %>");

 if(!isInteger (txtintval.value))

 {

args.IsValid=false;

 }

 Else

 {

args.IsValid=true;

 }

}

</script>

Include the above script tag in the head of your page.

Use the above validate function  for your custom validator clientside function as shown below.

 

Now instead of error message add the image tag as shown below.

<img src=alert.gif' style='width:15px;height:15px;'>

Note : Image tag doesn't have have an end tag. So don't add.

Then in test your page that shows images for errors.

See the picture below.

Know More:

You can also embed sound instead of error message or error image.

<asp:RequiredFieldValidator ControlToValidate="TextBox1" EnableClientScript="false" ID="RequiredFieldValidator1" runat="server" Text="<bgsound src='C:\Windows\Media\Windows Error.wav'>"></asp:RequiredFieldValidator>

Just make sure that the EnableClientScript="false"  when you want a sound instead of a text message.

Note: This technique doesn't work in firefox(Mozilla), Opera, Safari browsers when used with dotnet1.1 and Vs2003, but works with IE . However it works fine in all browsers (IE,Firefox,opera,safari) when used with dotnet 2.0 and VS2005. The reason is with cross browser compatibility of Asp.net validators i.e the emit script that is understood only by IE but not with other browsers. But now the issue is solved in higher versions.

The reason modern, non-Microsoft browsers do not support client-side validation in the validation controls is because of the client-side script emitted by BaseValidator. The client-side validation script emitted by BaseValidator includes, among other things, a JavaScript block that creates an array of the validators. This array, then, can be enumerated on the client-side script to check the validity of all of the validation controls. This array is registered in the BaseValidator's RegisterValidatorDeclaration() method, but unfortunately uses the client-side script document.all[validatorID]. An example of the array declaration when rendered on an ASP.NET Web page can be seen below:

<script type="text/javascript" language="javascript">
<!--
var Page_Validators = new Array(document.all["_ctl1"], document.all["_ctl2"], document.all["_ctl3"]);
// -->
</script>

The problem with document.all[ID] is that Microsoft's Internet Explorer is the only browser that supports this non-standard technique for referencing an element in the HTML document. The standard way (which Internet Explorer does support, along with Mozilla, Netscape, Opera, and others), is: document.getElementById(ID). So, the script emitted by the BaseValidator class will do nothing but generate JavaScript errors in non-Microsoft browsers.

You can use Dom validators to overcome this issue in dotnet1.1.

http://www.asp.net/community/control-gallery/Item.aspx?i=372

Note : If ur using validation summary then change the image tag for error display which is assigned to ErrorMessage property of the custom validator to Text property of the validator as the error message property will be used for error display in validation summary.
Now validate the universe.

Happing Coding

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Sudheendra Desai
C#, ASP.Net, SQL Server, Javascript, XML, CSS, Ajax,PHP,Design patterns
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
DevExpress Free UI Controls
Become a Sponsor
 Comments
You don't need to use the Text property by Jimmy On December 17, 2008
You can simply nest the content to display inside the validator server control tag. i think you will agree this is far more readable.
Reply | Email | Modify 
great by drinks On December 18, 2008
it's great!
Reply | Email | Modify 
ASP.NET image validator article by Patricia On April 23, 2010
Hi Sudheendra,

Thank you very much for this article. I looked all day for an article on this and couldn't find one. After many different searches I found your article.

It was very helpful for me. Thank you for doing it.

Patti
Reply | Email | Modify 
DevExpress Free UI Controls
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.