Create a custom validator in ASP. NET Atlas

Source: Internet
Author: User
Enlish version: http://dflying.dflying.net/1/archive/112_build_your_own_validators_in_aspnet_atlas.html

ValidatorYesASP. NET AtlasIs used to checkInputcontrolTypeAtlasControls, suchWeb. UI. textbox. If you are familiarASP. NETYou must knowASP. NETRunning as a server controlValidator.AtlasInValidatorProvides the same functions on the client.AtlasIncludes the following built-inValidator:

    1. Requiredfieldvalidator:Check whether data is input.
    2. Typevalidator:Check whether the input data is of a specific type.
    3. Rangevalidator:Check whether the input value is within a range.
    4. Customvalidator:Use a custom verification function to verify the input.
    5. Regexvalidator:Use the specified regular expression to verify the input.

AAtlasClient ControlValidatorCan be defined as a set, when the controlPropertychangedWhen an event is triggered,AtlasCallValidatorAllValidatorVerify the input data. Once the verification failsValidatorOfValidationmessageWill be set.ValidatorYou can verify the input of a group of controls in the form of a group and display the error information in a unified manner.

You can also specifyValidationerrorlabelThe control is associated with an input control that will be verified. It can display errors during verification and customize error prompts.

OK, Most of the above content comes fromAtlas. Let's useIpaddressvalidatorTo learn how to write customValidator. As the name suggests,IpaddressvalidatorIt is used to verify whether an input is a validIPv4Address.

Generally, write customValidatorThere are two steps:

    1. SlaveSYS. UI. validatorBase class.
    2. ImplementationValidate ()Method to verify the input, and return a Boolean value indicating whether the verification is successful.

Below isIpaddressvalidatorAnd save itIpaddressvalidator. 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 the aboveIpaddressvalidatorControl. AText BoxUsed for verificationIPAddress input, oneLabelDisplays error messages.

Here isAspxInHtmlDo not forgetScriptmanagerAdd to the aboveIpaddressvalidator. jsFile reference.

< 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 >

 

 

Below isAtlasScript definition:

< 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 >

 

Run the result in the browser:

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.