anti-keseronokan (old)
Fun Stuff for Anti-Fun People
Tuesday, November 22, 2005
Validator Controls in ASP.NET
One thing that I really like in the ASP.NET framework is the availability of simple to use validation controls. ASP.NET provides several type of validator controls such as the RequiredFieldValidator, CompareValidator, RangeValidator, and RegularExpressionValidator. And if that's not enough, there's also a CustomValidator where you can make your own rules. Back in my PHP days (not so far back), making a textbox to be mandatory filled up is quite a hassle. I need to write JavaScript to make sure it's not empty before sending, and then check back at the server-side in case the user is a smarty-ass and manage to bypass the JavaScript. With ASP.NET (using Visual Studio), I can drag-drop a RequiredValidator, specify which control to validate (maybe a textbox, a dropdown list, or whatever), change the text to say "Hey dude, we need your name. Please type it in!" as the error message and we're done! JavaScript checking is done for you. At the server side, check the boolean value Page.IsValid to make sure all validations passed with flying colors before proceeding with inserts/updates routines etc. You can list all errors using ValidationSummary control, or even make it to popup an alert() box. If you wanna be naughty and really get down and dirty, you can look at "C:\Inetpub\wwwroot\aspnet_client\system_web\1_1_4322\WebUIValidation.js". This is the generic JavaScript file that supply all validation functions. Your custom control can also be made validatable (is that a legal word?). Put the ValidationPropertyAttribute in your custom control declaration so a validator can hook-up on it, like so :
[ValidationPropertyAttribute("Number")]
public class PhoneNumber : WebControl, INamingContainer
.
.But this one is quite tricky (making a custom control is always tricky), especially for composite controls.
I don't have time to tell more, but there should be abundant resources on the Internet that you can read up. Some links for you.
- Google result on "asp.net validator"
- MSDN page on "BaseValidator" (the parent of all validators)
- An article that I found

1 Comments:
sokong ko..
hahah...asp.net more better
ish..lepaih nih pakai PHP
tak suke nye...ish ish..JavaScript plak..ish..ish
bukan leh pakai drag&drop jek..ish..ish..
Post a Comment
<< Home