Geeks With Blogs
Sandu's Blog Views on life as a techie

Recently I used a ASP.NET CustomValidator to check for the length of text entered into a TextBox. I used a ValidationSummary Control to show the value of the errortext property in the CustomValidator. The requirement was to truncate the text to the allowed limit and show the error when the submit button is clicked. If the submit button is clicked again the page will submit without error if the other Validation Controls on the page are also valid.

The problem with doing this sort of thing is that if you truncate the text inside the CustomValidator's javascript function the page will submit at once without showing any error.

The solution was something as follows, where I added a second javascript function for the button's onclick event.

function ValidateLength() { var DescriptionText = document.Form1.DescriptionTextBox.value; if(DescriptionText.length > 200){ if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate(); document.Form1.DescriptionTextBox.value = DescriptionText.substr(0, 200); return false; } else{ if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate(); } }

the custom validator's function looks like follows;

function ValidateMaxLength(sender, args) { var DescriptionText = args.Value; if(DescriptionText.length > 200){ args.IsValid = false; } else { args.IsValid = true; } }

Posted on Friday, June 2, 2006 9:20 AM | Back to top


Comments on this post: Customizing ASP.NET Validation Controls

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Sandaruwan Fernando | Powered by: GeeksWithBlogs.net