Archive

Archive for the ‘Flex’ Category

IP Validator Component.

February 9, 2009 Leave a comment

package
{
import mx.validators.ValidationResult;
import mx.validators.Validator;

//Class should extend mx.validators.Validator
public class IPAddressValidator extends Validator {

public function IPAddressValidator() {
// Call base class constructor.
super();
}

// Class should override the doValidation() method.
// doValidation method should accept an Object type parameter
override protected function doValidation(value:Object):Array {
// create an array to return.
var ValidatorResults:Array = new Array();
// Call base class doValidation().

ValidatorResults = super.doValidation(value);
// Return if there are errors.

if (ValidatorResults.length > 0)
return ValidatorResults;

if (String(value).length == 0)
return ValidatorResults;

var RegPattern:RegExp = /\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/;

var a:Array = RegPattern.exec(String(value));

if (a == null){
ValidatorResults.push(new ValidationResult(true, null, “IPAddress Error”,”You must enter an IP Address”));
return ValidatorResults;
}
return ValidatorResults;
}
}
}

<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute”
xmlns:com=”*” xmlns:flexScript=”flexScript.*”>
<mx:panel>
<mx:Form>
<mx:FormHeading label=”IP Validator” textAlign=”left” width=”100%”/>
<mx:FormItem label=”Enter IP Address”>
<mx:HBox width=”100%” horizontalAlign=”right” verticalAlign=”middle”>
<mx:TextInput id=”IPInput”  maxChars=”15″ restrict=”0-9.”/>
</mx:HBox>
</mx:FormItem>
<mx:FormItem width=”100%”>
<mx:HBox width=”100%” horizontalAlign=”right” verticalAlign=”middle”>
<mx:Button id=”button” label=”Submit”/>
</mx:HBox>
</mx:FormItem>
</mx:Form>
</mx:panel>
<com:IPAddressValidator source=”{IPInput}” property=”text”/>
</mx:Application>

Categories: Flex Tags:
Follow

Get every new post delivered to your Inbox.