I was creating a simple application to enter IP addresses into a database and I found there was no simple way to validate the IP addresses. With Flex it is fairly simple to create a custom validator. The code below uses a regular expression to validate each octet of the IP to make sure it is in the range of 0-255. For an example of usage you can use any of the examples within the Flex documentation when using Validators.
package validators
{
import mx.validators.Validator;
import mx.validators.ValidationResult;
public class IPValidator extends Validator {
// Define Array for the return value of doValidation().
private var results:Array;
// Constructor.
public function IPValidator() {
// Call base class constructor.
super();
}
// Define the doValidation() method.
override protected function doValidation(value:Object):Array {
// Clear results Array.
results = [];
// Call base class doValidation().
results = super.doValidation(value);
// Return if there are errors.
if (results.length > 0)
return results;
if (String(value).length == 0)
return results;
var pattern: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 r:Array = pattern.exec(String(value));
if (r == null)
{
results.push(new ValidationResult(true, null, "NaN",
"You must enter an IP Address"));
return results;
}
return results;
}
}
}
I added to it...
package com.domain.utils { import mx.validators.Validator; import mx.validators.ValidationResult; public class URLValidator extends Validator { private var results:Array; public function URLValidator() { super(); } private function validateIP(str:String):Boolean { var pattern: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 r:Array = pattern.exec(str); if (r == null) { return false; } return true; } override protected function doValidation(value:Object):Array { results = []; results = super.doValidation(value); if (results.length > 0) { return results; } var theStringValue:String = (String(value).toLowerCase()); if (theStringValue.length == 0) { return results; } if ((theStringValue.indexOf('http') == -1) && (theStringValue.indexOf('https') == -1) && (validateIP(theStringValue) == false) ) { results.push(new ValidationResult(true, null, "NaN", "You must enter a valid 'http' prefix, valid 'https' prefix or valid IP Address")); return results; } if (( (theStringValue.indexOf('http') != -1) || (theStringValue.indexOf('https') != -1) ) && (theStringValue.indexOf(':') == -1) ) { results.push(new ValidationResult(true, null, "NaN", "You must enter a ':' in your web address after 'http' or 'https'")); return results; } if (( (theStringValue.indexOf('http') != -1) || (theStringValue.indexOf('https') != -1) ) && (theStringValue.indexOf('//') == -1) ) { results.push(new ValidationResult(true, null, "NaN", "You must enter a '//' in your web address after 'http:' or 'https:'")); return results; } if ((theStringValue.indexOf('.') == -1) ) { results.push(new ValidationResult(true, null, "NaN", "No '.' exists in your website address")); return results; } var wwwArr:Array = theStringValue.split('.'); if ((wwwArr.length < 3 && (theStringValue.indexOf('www') != -1) ) ) { results.push(new ValidationResult(true, null, "NaN", "A '.' is missing after the 'www'")); return results; } var suffixDotpos:Number = theStringValue.lastIndexOf("."); var suffixStr:String = theStringValue; var theSuffix:String = suffixStr.substring(suffixDotpos,suffixStr.length); if (theSuffix.length < 3 && (validateIP(theStringValue) == false) ) { results.push(new ValidationResult(true, null, "NaN", "The domain suffix in your website address is invalid.")); return results; } return results; } } }
Hi
How do i use any validator class here for eg IPValidator in my coustom class which is used to build a combox showing time. I want the validator to validate time entred by used.
How can use validator in my coustom class?
Can any one help me.
Thanks in advance, Parag
1. TextInput creates dynamically
textInputBox = new MyTextInput;
textInputBox.restrict = “0-9.”; textInputBox.maxChars = 24; amountValidator = new NumberValidator(); amountValidator.source = textInputBox; amountValidator.property = “text”; amountValidator.allowNegative = false; amountValidator.domain = “real”; amountValidator.precision = 4; amountValidator.required = false; amountValidator.maxValue = 999999999999.9999; amountValidator.trigger = textInputBox; amountValidator.triggerEvent = Event.CHANGE; amountValidator.addEventListener(ValidationResultEvent.VALID, amountValid); amountValidator.addEventListener(ValidationResultEvent.INVALID, amountInvalid);
private function amountValid(event:ValidationResultEvent):void { valid = true; fieldsValidated = true; }
private function amountInvalid(event:ValidationResultEvent):void {
valid = false; fieldsValidated = true; }
2. As mention in the creation, when we exceed the limit, it shows error my red color border, and the same time if you delete them by DEL key when it come to the given acceptable limit, automatically become to green soon. 3. Leave from the field and change values of another textinput(this is just a textinput, this is a form there are some more form elemets), then come back to value exceeded textfield by SHIFT+TABS and remove the additional entered numbers, when you come to green soon your value is accepted. 4.Now again enter more values and now you are in the warn zone, then leave from the field and do the few changes in other form elements. 5. Then come back to the value exceeded text filed by MOUSE CLICK, and start delete from DEL, even though you removed additional values, still fields shows that you are in warn zone.
Actual Results: Even when remove additional numbers,still field is Red
Expected Results: if remove additional numbers, field should come its normal status.
Picture of this issue can be viewed a href=”http://bugs.adobe.com/jira/browse/SDK-24372