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:
- Requiredfieldvalidator:Check whether data is input.
- Typevalidator:Check whether the input data is of a specific type.
- Rangevalidator:Check whether the input value is within a range.
- Customvalidator:Use a custom verification function to verify the input.
- 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:
- SlaveSYS. UI. validatorBase class.
- 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: