|

Introduction
I've been playing with ASP.NET since Beta 1, and I have to admit that I love it! What used to take days in traditional ASP can now be done in an afternoon. One of the few complaints that I have is ASP.NET's implementation of validators. While I certainly believe that validation controls extremely are time saving, I'm not really thrilled with the ones provided by Microsoft. They work great if you only have a few items to validate, but can become a chore to maintain, when there are many fields that need validation.
What I wanted was a single control that you could drop on to your form, that would handle all the validation for you. This way, you have one validator to maintain, not 10, or whatever. So I wrote this custom Validator control. Included in the download is the Validator control itself, its source code (don't run away screaming now, it's in VB.NET...), and 3 JavaScript files. I couldn't find an elegant way to use Microsoft's JavaScript files, and they didn't seem to support the DOM specs, so I wrote my own files. These need to be placed in your \inetpub\wwwroot\_vti_script directory. Validation.js is the main JavaScript file, which sets up some prototype functions, then links to one of the other two files depending on your browsers capability. ValidationDOM.js is, of course, for DOM compliant browsers (NS6.2+, IE5+). ValidationIE.js is for earlier versions of IE. I chose not to support early versions of Netscape, because there doesn't seem to be an elegant version of document.getElementById or document.all, and so Validation.js simply forces early versions of Netscape to use server side validation only.
Using the control
That being said, an explanation of its use is in order. The Validator control is derived from System.Web.UI.Webcontrols, and so has the standard properties of a web control. It also has 4 additional properties:
HeaderText As String
The text that is displayed at the beginning of the error summary report.
ListStyle As Enum
The style of bulleting that the error summary will use.
ClientSideScript As Boolean
Produces code to allow validation at the client side.
UniqueErrors As Boolean
Ensures that all strings in the errors collection are unique.
Once the Validator has been added to a page, you need to tell it what to validate. By either right-mouse clicking on the control, or looking at the designer verbs area just below the property window, you'll notice a menu/verb item marked 'Edit Fields'. A dialog then appears (as shown in the screenshot) allowing you to modify the control's collection of validations. Clicking on one of the buttons, adds a validator to the list, while clicking Remove will, of course, remove the selected item. All validation options share some common properties:
ControlToValidate As String
The ID of the control to validate. The Validator control automatically appends any namespace on, so you just need the controls ID.
ErrorMessage As String
The message to display if this validation proves invalid
ErrorMessageTarget As String
The ID of the control where you want the ErrorMessage to be displayed. This is useful when you want the error message to appear somewhere on your form other than the summary.
ShowInSummary As Boolean
Determines whether or not the ErrorMessage should be displayed in the summary. If this is false, and you set the ErrorMessageTarget property, then the ErrorMessage only appears at the ErrorMessageTarget.
Next is a summary of the additional properties of each of the validation types:
ReqFieldValidator - Verifies that a field has been modified
RegExFieldValidator - Verifies that a fields value matches a specific pattern
RegularExpression As String
The regular expression to use for validation.
CaseSensitive As Boolean
If True, then case will matter. So, with a pattern of "[A-Z]{3}", AAA will be valid, but AaA will not.
MultiLine As Boolean
Allows pattern matching to span more than one line of an HTML TextArea field.
RangeFieldValidator - Verifies that a field's value lies within a certain range
Minimum As String
The minimum value. This is a string, because the Validator supports characters as well as numbers.
Maximum As String
The maximum value.
Type As String
The type of value that you'll be comparing. Possible values are String, Integer, Double, and DateTime
IgnoreCase As Boolean
Whether to ignore case or not. If IgnoreCase is true, Minimum is A, and Maximum is Q, then f will be considered valid.
CompareFieldValidator - Verifies that a field's value compares a specific way to another value
Operator As Enum
Determines what kind of comparison to perform. Possible values include (but are not limited to) DataTypeMatch, GreaterThan, LessThan, etc.
Type As String
The type of value that you'll be comparing. Possible values are String, Integer, Double, and DateTime.
ControlToCompare As String
The control to compare against.
ValueToCompare As String
The value to compare against. If ControlToCompare and ValueToCompare both have values, then ControlToCompare takes precedence.
IgnoreCase As Boolean
Whether to ignore case or not.
CustomFieldValidator - Verifies a field's value using a custom function
ClientSideFunction As String
The name of the client side function to use in validating. This function must take a single parameter that is the control specified by the ControlToValidate property, and must return true for valid and false for invalid.
Name As String
The Validator control raises a single event when server side validation takes place. This property allows you to distinguish between different custom validations.
In order to use server side validation for the CustomFieldValidator, you need to handle the CustomFieldValidation event of the Validator object. All CustomFieldValidator items that you've added to the control will route through this same event, so you need a way to distinguish between them. This is where the Name property of the CustomFieldValidator comes in hand. It is sent in the CustomFieldValidatorEventArgs class to the event handler. Public Sub Validator1_CustomFieldValidation(ByVal sender As Object, _
ByVal E As CP.Validator.CustomFieldValidatorEventArgs) _
Handles Validator.CustomFieldValidation
Select Case E.Name
Case "IsEven"
If CInt(DirectCast(E.ControlToValidate, _
TextBox).Text) Mod 2 = 0 Then
E.IsValid = True
Else
E.IsValid = False
End If
End Select
End Sub
To add a validation to the control at run time, use the Validators collection of the control: Validator1.Validators.Add(New ReqFieldValidator("txtName",
"Enter a name", ""))
I've tested it every way that I could think of, and it seems to be stable. However, since this is the control's first release to the public, you should expect bugs. If you run across any, please either post them here, or E-mail me directly at jamie.nordmeyer@mcgnw.com, and I'll do my best to fix it. Also, if you have any comments, concerns, or suggestions, let me know.
| You must Sign In to use this message board. |
|
| | Msgs 1 to 25 of 41 (Total in Forum: 41) (Refresh) | FirstPrevNext |
|
 |
|
|
Jamie, I am trying to use the CustomFieldValidation to check if atleast 1 of 2 checkboxes have been selected. I read in your article about adding a validation control at runtime and tried it and did not have success.
This if what I am doing. The 2 checkboxes are called chkSystemic and chkIndividual, and I need to check that atleast one of them has been selected.
On the Page_Load event, I have this code Validator1.Validators.Add(New CustomValidator("chkSystemic", "Enter a Degree", ""))
But I get an error "Too many arguments to 'Public Sub New()'."
Can you please tell me what I am doing wrong? I also tried using a Javascript function, but did not have any luck Thanks - Richie
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I'll take a look this weekend and see if I can figure out what's going on (I won't have time until then unfortunately).
Kyosa Jamie Nordmeyer - Taekwondo Yi (2nd) Dan Portland, Oregon, USA www.defaultn.com
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Jamie, I had another question. Can I validate a group of elements and if so how do I do it. Basically, I have 5 checkbox's on the page and I need to validate that atleast one of the 5 check boxes have been selected before submitting the form. How do I do this using only one Validator control? Or do I have to have a separate Validator control for each of the check boxes Thanks - Richie
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
|
You'll need to use the CustomFieldValidator to do this. The .NET 2.0 version of the control has this type of validation already planned for a near future release. If I find the time, I'll try to add it to this version as well, but with my current schedule, it may take a while.
For now, hook up a CustomFieldValidator, and use a javascript function to confirm the number of checked boxes.
Kyosa Jamie Nordmeyer - Taekwondo Yi (2nd) Dan Portland, Oregon, USA www.defaultn.com
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
Jamie, This is a very useful control and I have used it in a couple of applications and it works great.
However, I am facing a problem and not able to figure it out. In the first application, I used the control and was able to change the font size of the error message to X-Small or XX-Small. The font size did not change in design mode, but while running the application the error message was displayed in a smaller font. Now, I am using this control in the 2nd application, and the font size does not change during runtime. I compared the control properties on both these projects and they are the same. Any ideas why the control is not working correctly in the 2nd project.
FYI, both projects are being developed in VB.NET2003 Thanks - Richie
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hey there. Firstly, I'm glad the control has been useful for you. Always nice to know that your hard work is being used. 
I haven't honestly worked in .NET 1.1 for a while now, so I'll have to take a few to get used to this version of the control again (been working the 2.0 version lately), but I'll have an answer for you as soon as I possibly can. Sometime this week. If you figure it out before I do, please post it, and I'll make sure the change gets published to the download. Otherwise, I'll get one out as soon as I can.
Take it easy!
Kyosa Jamie Nordmeyer - Taekwondo Yi (2nd) Dan Portland, Oregon, USA www.defaultn.com
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Jamie, Thanks for looking into this issue.
I have found a temporary way to handle the problem. I modified the "ShowErrorMessage" function in the 2 javascript files, by adding the font-size as the style
Original html="<SPAN STYLE=\"color: red;\">";
New html="<SPAN STYLE=\"color: red; font-size:11;\">";
So for now it works. But if you can fix the problem, wherein I can change the font size from the design environment, that would be great. I am still curious how it worked in one project and not another. Thanks - Richie
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Finally! I've been busy, what with a new girlfriend (love the woman!!!), a new job, and a daughter in school, but I'm finally near to releasing the .NET 2.0 version of this control (the .NET 1.1 version will still be available for download). I'll hopefully have it up within a couple weeks.
I decided to go with a complete rewrite of the control, since .NET 2 provides many new features that didn't exist when I wrote this 1st verion. This being the case, if you're using the 1.1 version of the control, and decide to use the 2.0 control, you'll need to set everything back up again. Full documentation will be provided, and I'll get an official web site up, even if it's just a blog site.
Also, I'd love to hear ideas from people about validation conditions that they'd like added. When I initially release this version, it'll have the following conditions:
• ChangeRequiredCondition - Synonymous with .NET's RequiredCondition (I felt the name was more reflective of what it does. • RegularExpressionCondition • RangeCondition • CompareCondition • ModulusCondition - Just verifies that a field is divisible by a specific number. • CustomCondition - Will contain support for AJAX callbacks.
Slated for version 2.1 will be the following: • SummationCondition - Only valid if the assigned array of controls sum to a specific number • OrCondition - Wraps a group of conditions so that if ANY of them are valid, the group is valid (most useful with the ChangeRequiredCondition, um, condition. :->
Thanks to all for the feedback that I've received about the 1.1 version, and I'm glad that it's been useful. Feel free to fire any ideas that you have at me (perverbially, I DO have a kid to raise), and I'll see what I can do.
Kyosa Jamie Nordmeyer - Taekwondo Yi (2nd) Dan Portland, Oregon, USA
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Thanks Kyosa
Great control. But only i want to ask is we need to include 3 javascript. Doesn't it increase the size of the page. Also please let me know the tentative date of releasing your library/control.
Thanks, Manish
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi Manish. I'll be releasing a beta version within the next week. There are still some issues that I need to work out (which will be outlined in the article that accompanies the control).
As for Kyosa, my first name is actually Jamie. Kyosa is a Taekwondo title that means "Teacher".
Kyosa Jamie Nordmeyer - Taekwondo Yi (2nd) Dan Portland, Oregon, USA
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
Thanks! I'm in the process of rewriting it for ASP.NET 2.0, which will remove the need to maintain external javascript files (thanks to the WebReference attribute), as well as make the control more customizable and add in new validators. Hopefully I'll have it up soon.
Kyosa Jamie Nordmeyer - Taekwondo Yi (2nd) Dan Portland, Oregon, USA
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
HI, I really Like The Idea and It is an Excellent Control If This Controls Supports Different Styles Like The Style For Whole Summery Control and Style For Individual Error Messages. Secondly How can we Display The Message Next To The Controls OR IF The Style Of The Control Which is not Validated will be changed Like If The Style is LoginTextBox and if not Validated it will be changed to LoginTextBox_Error. Then It will Be really helpful and Control's Usability will be increased.
Asif Raza Ashraf
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi Asif. Firstly, thanks for the kudo's on the control. Currenly, the control doesn't support different stylings. However, I've been thinking about rewriting it from ground-up. Your requests sound like great ones, and I think I definitely need to rewrite it now. Sorry it doesn't quite work for your needs yet, but hopefully it won't take long to do.
Kyosa Jamie Nordmeyer - Taekwondo Yi (2nd) Dan Portland, Oregon, USA
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
|
Yes it is a first class control, but I too am finding some issues runnign this from .NET 2.0.
The only other suggestion I could make is integrating which form controls trigger the validation...i still have to go thru the form and click each form control to indicate which ones will support the validator.
The other item i might make some grief about is having to hard code the .js path. I really would like to be able to put the js wherever i like. If i run this in .NET 2.0 from the local VS web browser there is no vti folder and you can't create one due to it being a system folder.
Being able to custom define the js path would be a big plus from my perspective.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I agree completey with you having to hardcode the path to the .js files. As I'll be rewriting this in C#, using the 2.0 framework, I'll be using the WebResource attribute to keep the .js files in the assembly, so you don't have to use or specify the path.
I've already started on the control. Hopefully I'll have it available in the next month or so.
Kyosa Jamie Nordmeyer - Taekwondo Yi (2nd) Dan Portland, Oregon, USA
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
This is a great control, I want to use it in my project. Here is what I have done:
1. Copy dll to my bin folder, add reference to the dll, register it in the tool bar 2. Drag and drop it into the aspx file in the designer 3. Add a ReqFieldValidator in my page_load event 4. put three .js files into wwwroot/_vti_script folder
Run the file, only the server side is working, the client side validation is not working.
Here is my code:
Aspx file:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="demo1.aspx.vb" Inherits="Chris.demo1"%> <%@ Register TagPrefix="cp" Namespace="CP" Assembly="Validator" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <title>demo1</title> <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> </HEAD> <body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"> Choose an item:<br> <asp:DropDownList id="ddlItems" runat="server"></asp:DropDownList> <br> <br> If other, please specify:<br> <asp:TextBox id="txtOther" runat="server"></asp:TextBox><br> <br> <asp:Button id="btnSubmit" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 128px" runat="server" Text="Submit"></asp:Button><br> <br> <asp:Label id="lblOk" EnableViewState="false" runat="server" style="Z-INDEX: 102; LEFT: 24px; POSITION: absolute; TOP: 176px" /> <br> <br> <cp:Validator id="Validator1" runat="server"></cp:Validator> </form> </body> </HTML>
Vb file:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Put user code to initialize the page here If Not Page.IsPostBack Then For i As Integer = 1 To 10 ddlItems.Items.Add(New ListItem("Item " + i.ToString, i.ToString())) Next ddlItems.Items.Add(New ListItem("Other", "11")) End If
Validator1.Validators.Add(New CP.ReqFieldValidator(txtOther.ClientID, "Enter First Name", ""))
End Sub
Please help.
Thanks
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hey there, rugrats. I'll take a look today, and get back to you. Didn't want to leave you hanging, though.
Kyosa Jamie Nordmeyer - Taekwondo Yi (2nd) Dan Portland, Oregon, USA
|
| Sign In·View Thread·PermaLink | 2.00/5 (3 votes) |
|
|
|
 |
|
|
Hi,
I loved your control, however I'd like to use it easily with multilangual site design. I prefere to use Localization solution suggested by Karl Seguin. http://www.codeproject.com/aspnet/LocalizedSamplePart2.asp How can I integrate your code to his. I made few tries but failed so far. I'm not as pro as you guys are, however I didn't give up yet 
I managed problem I stated just below. It was my ignorance on how to add a new project to a solution. It doesn't mean I managed to make it work though. I'll keep you notified. /* One of the strange things that I see while I was trying to understand your code is System.web.UI.Design gives error. I use Framework 1.1 (VS2003). In documentation it says this class exists in Framework 1.1, yet I don't see it in intellisense. I don't get this error when I look at your project, but I get it when I try to integrate it to Karl's Localized class. */
Another thing I see is that you wrote your code in framework 1.0 (If I get it right). Would it make it impossible to integrate to Karl's code?
Thanks
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hey there. Unfortunately, localization isn't something that I've played with much, so I can't answer your question right off the top of my head. I'll try to look at it when I get a chance, but I'm also extremely busy lately. Hopefully I can provide an answer soon.
Kyosa Jamie Nordmeyer - Taekwondo Yi (2nd) Dan Portland, Oregon, USA
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
hi, i have text box in my aspx page and my requirement is only charectors i.e from (A to Z and a to z) can be enter in that textbox. thanks in advance. regards M.farooq
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
|
No problem, M. Add a RequiredFieldValidator node to the Validator control, and set its RegularExpression property to "[A-Za-z]". This is a regular expression range for all alpha charectors.
Good luck!
Kyosa Jamie Nordmeyer - Yi Dan Portland, Oregon, USA
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
|
General News Question Answer Joke Rant Admin
|