package com.komponenty { import mx.resources.ResourceBundle; import mx.resources.ResourceManager; import mx.validators.ValidationResult; import mx.validators.Validator; public class KodPocztowyValidator extends Validator { loadResources(); [ResourceBundle("validators")] private static var packageResources:ResourceBundle; private static var resourceWrongFormatError:String; private static var resourceInvalidCharError:String; private static var resourceWrongLengthError:String; //-------------------------------------------------------------------------- // // Class methods // //-------------------------------------------------------------------------- /** * @private * Loads resources for this class. */ private static function loadResources():void { //ResourceManager.getInstance().localeChain = ["pl_PL"]; resourceInvalidCharError = ResourceManager.getInstance().getString("validators","InvalidCharError"); resourceWrongLengthError = ResourceManager.getInstance().getString("validators","wrongLengthError"); resourceWrongFormatError = ResourceManager.getInstance().getString("validators","wrongFormatError"); } public static function validateKodPocztowy(validator:KodPocztowyValidator, value:Object, baseField:String):Array { var results:Array = []; var zip:String = String(value); var len:int = zip.length; var n:int; var i:int; // Sprawdz czy jest ciąg znakow zawiera prawidłowy format for (i = 0; i < len; i++) { if (DECIMAL_DIGITS.indexOf(zip.charAt(i)) == -1 && zip.charAt(i) != "-") { results.push(new ValidationResult( true, baseField, "resourceInvalidCharError", validator.invalidCharError)); return results; } } //Sprawdz czy format jest prawidlowy if (len == 6) { if ((DECIMAL_DIGITS.indexOf(zip.charAt(0)) == -1)|| (DECIMAL_DIGITS.indexOf(zip.charAt(1)) == -1)|| (zip.charAt(2)!="-")|| (DECIMAL_DIGITS.indexOf(zip.charAt(3)) == -1)|| (DECIMAL_DIGITS.indexOf(zip.charAt(4)) == -1)|| (DECIMAL_DIGITS.indexOf(zip.charAt(5)) == -1) ) { results.push(new ValidationResult( true, baseField, "wrongFormatError", validator.invalidFormatError)); return results; } } else { results.push(new ValidationResult( true, baseField, "wrongLength", validator.wrongLengthError)); return results; } return results; } public function KodPocztowyValidator() { super(); bundleChanged(); } //-------------------------------------------------------------------------- // // Properties: Errors // //-------------------------------------------------------------------------- [Inspectable(category="Errors", defaultValue="Nieprawidłowe znaku w kodzie pocztowym")] public var invalidCharError:String; [Inspectable(category="Errors", defaultValue="Kod pocztowy musi być 6 znakowy w formacie 99-999")] public var invalidFormatError:String; [Inspectable(category="Errors", defaultValue="Zbyt duża ilość znaków w kodzie pocztowy")] public var wrongLengthError:String; //-------------------------------------------------------------------------- // // Overridden methods // //-------------------------------------------------------------------------- override protected function doValidation(value:Object):Array { var results:Array = super.doValidation(value); // Return if there are errors // or if the required property is set to false and length is 0. var val:String = value ? String(value) : ""; if (results.length > 0 || ((val.length == 0) && !required)) return results; else return KodPocztowyValidator.validateKodPocztowy(this, value, null); } //-------------------------------------------------------------------------- // // Methods // //-------------------------------------------------------------------------- /** * @private * Populates localizable properties from the loaded bundle for this class. */ private function bundleChanged():void { invalidCharError = resourceInvalidCharError; wrongLengthError = resourceWrongLengthError; invalidFormatError = resourceWrongFormatError; } } }
Programowanie c++/c#/delphi/javas/php/installscript/nativescript
Przykłady - przydatne funkcje
wtorek, 27 kwietnia 2010
Flex - Walidator kodu pocztowego z pola tekstowego
Klasa Walidatora oparty na standardowym walidatorze - wykorzystująca lokalne źródła komunikatów
Etykiety:
Flex-walidatory
Subskrybuj:
Komentarze do posta (Atom)
Brak komentarzy:
Prześlij komentarz