Menu:

Recent Entries

Categories

Archives

Links

Blogs
- Dflying's Night
- David's Untitled Life
- Dflying's Blog in Chinese

This blog is hosted by DreamHost!

Syndicate

RSS 0.90
RSS 1.0
RSS 2.0
Atom 0.3

Build Your Own Validators in ASP.NET Atlas

Dflying | 06 April, 2006 20:08

Validators are a powerful way of checking the data entered by the user into ASP.NET Atlas client side controls of type InputControl, such as the Web.UI.TextBox control. If you are familiar with ASP.NET, you must know the validators provided by ASP.NET as server controls. Atlas validators provide the same behavior but runs totally on client side. There are a number of built-in validators:

  1. requiredFieldValidator. Checks that data was entered.
  2. typeValidator. Checks the type of the data, such as Number.
  3. rangeValidator. Checks the input value between an upper and lower bound.
  4. customValidator. Defines a custom expression handler.
  5. regexValidator. Checks the data using a regular expression. The regular expression value must be delimited with "/" characters.

Validators are defined through a collection. When the propertyChanged event is raised, the control value is validated and the validate event raised. You can handle the validated event if you like. During validation, the validators are queried and might result in the client control's invalid and validationMessage properties being set.

A special control of type validationErrorLabel can be used to display the error message using a tooltip or an asterisk (*). Validators can also be grouped so that you can check validity for a group of controls as a unit.

Ok, I am not going to introduce how to use validators today. Let’s see how to build your own validators by the following IPAddressValidator demo, which is used to validate if the input is a valid IP Address.

Generally, there are two steps to build a custom validator:

  1. Inherits form Sys.UI.Validator base class.
  2. Implements the validate() method to validate the input value and returns a Boolean value which indicate whether input is valid.

Here’s the code for IPAddressValidator. Save it as IPAddressValidator.js.

Sys.UI.IPAddressValidator = function() {
    Sys.UI.IPAddressValidator.initializeBase(this);
 
    this.validate = function(value) {
        if (!value) {
            return false;
        }
        if (String.isInstanceOfType(value)) {
            if (value.length == 0) {
                return false;
            }
        }
        var ipPattern = /^(d{1,3}).(d{1,3}).(d{1,3}).(d{1,3})$/;
        var ipArray = value.match(ipPattern);
 
        if (ipArray == null)
            return false;
 
        for (i = 0; i < 4; i++) {
            var thisSegment = ipArray[i];
            if (thisSegment > 255) {
                return false;
            }
        }
        return true;
    }
}
Sys.UI.IPAddressValidator.registerSealedClass('Sys.UI.IPAddressValidator', Sys.UI.Validator);
Sys.TypeDescriptor.addType('script', 'ipAddressValidator', Sys.UI.IPAddressValidator);

Let’s test our IPAddressValidator in a page. We add a text box for the IP Address input and a label for showing error message.

Here is the HTML definition in ASPX. Do not forget to add a reference to our IPAddressValidator.js in ScriptManager.

<atlas:ScriptManager runat="server" ID="ScriptManager1">
    <Scripts>
        <atlas:ScriptReference Path="IPAddressValidator.js" />
    </Scripts>
</atlas:ScriptManager> 
  
<div class="description">
    Please input an IP Address: 
    <input type="text" id="ipBox" class="input" />
    <span id="ipValidator" style="color: red">This IP Address is invalid!</span>
</div>

And here is the Atlas script.

<page xmlns:script="http://schemas.microsoft.com/xml-script/2005">
    <components>
        <textBox id="ipBox">
            <validators>
                <ipAddressValidator errorMessage="This IP Address is invalid!" />
            </validators>
        </textBox>
        <validationErrorLabel id="ipValidator" associatedControl="ipBox" />
    </components>
</page>

Runs in browser:

Posted in Atlas. Comment: (2). Trackbacks:(89). Permalink
«Next post | Previous post»

Referers

Comments

  1. 1. best online gambling site  |  12/28,2009 at 04:47

    I would suggest using dependency injection to pass a class that provides the means for retrieving the current user to make this audit implementation more usable from different environments.

  2. 2. Download Windows  |  01/06,2010 at 04:59

    I think most of the comments here miss the point. This isn't about Java vs. C#. It's about Cocoa vs. .NET (and perhaps slightly less about C# vs. Objective-C). Now that I've developed with C# and .NET, I can seriously say that I much prefer Cocoa.

Leave a Reply

Auth Image