SocialSecurityNumberValidator class
The SocialSecurityNumberValidator class validates that a String is a valid United States Social Security number. It does not check whether it is an existing Social Security number.
class SocialSecurityNumberValidator extends WidgetValidator { /** * Specifies the set of formatting characters allowed in the input. * By default equals "-" */ String allowedFormatChars; /** * Error message when the value contains characters other than digits and * formatting characters defined by the [allowedFormatChars] property. * By default equals "You entered invalid characters in your Social Security number." */ String invalidCharError; /** * Error message when the value is incorrectly formatted. * By default equals "The Social Security number must be 9 digits or in the form NNN-NN-NNNN." */ String wrongFormatError; /** * Error message when the value contains an invalid Social Security number. * By default equals "Invalid Social Security number; not allowed all zeros in any digit group." */ String zeroGroupError; /** * Error message when the value contains an invalid Social Security number. * By default equals "Invalid Social Security number; not allowed Area Numbers 666 and 900-999." */ String wrongAreaError; SocialSecurityNumberValidator(Widget widget, { this.allowedFormatChars:"-", this.invalidCharError:"You entered invalid characters in your Social Security number.", this.wrongFormatError:"The Social Security number must be 9 digits or in the form NNN-NN-NNNN.", this.zeroGroupError:"Invalid Social Security number; not allowed all zeros in any digit group.", this.wrongAreaError:"Invalid Social Security number; not allowed Area Numbers 666 and 900-999." }) : super(widget); /** * Executes the validation logic of this validator to validate a Social Security number. * For an invalid result returns an List of [ValidationResult] * objects, with one ValidationResult object for each field examined by the * validator that failed validation. * * You must override this method in a subclass of a validator class. */ List<ValidationResult> doValidation(dynamic value) { List<ValidationResult> results = super.doValidation(value); // Return if there are errors or if the required property is set to false // and length is 0. String val = value != null ? value.toString() : ""; if (results.length > 0 || ((val.length == 0) && !required)) return results; else return _validateSocialSecurity(value); } /** * Validate [value] and return List of [ValidationResult] objects, with one * ValidationResult object for each field examined by the validator. */ List<ValidationResult> _validateSocialSecurity(dynamic value) { List<ValidationResult> results = new List<ValidationResult>(); // Resource-backed properties of the validator. int len = value.length; bool checkForFormatChars = false; if ((len != 9) && (len != 11)) { results.add(new ValidationResult(true, wrongFormatError)); return results; } for (int i = 0; i < allowedFormatChars.length; i++) { if (Validator.DECIMAL_DIGITS.indexOf(allowedFormatChars[i]) != -1) { throw new Exception("Invalid format chars"); } } if (len == 11) checkForFormatChars = true; for (int i = 0; i < len; i++) { String allowedChars; if (checkForFormatChars && (i == 3 || i == 6)) allowedChars = allowedFormatChars; else allowedChars = Validator.DECIMAL_DIGITS; if (allowedChars.indexOf(value[i]) == -1) { results.add(new ValidationResult(true, invalidCharError)); return results; } } String _value = value.replaceAll("-", ""); String area = _value.substring(0, 3); String group = _value.substring(3, 5); String serial = _value.substring(5, 9); // Not allowed are SSNs with all zeros in any digit group // (000-xx-####, ###-00-####, ###-xx-0000). if (area == "000" || group == "00" || serial == "0000") { results.add(new ValidationResult(true, zeroGroupError)); return results; } // Not allowed are SSNs with Area Numbers 000, 666 and 900-999. int areaNumber = int.parse(area); if (areaNumber == 666 || (areaNumber >= 900 && areaNumber <= 999)) { results.add(new ValidationResult(true, wrongAreaError)); return results; } return results; } }
Extends
Validator > WidgetValidator > SocialSecurityNumberValidator
Constructors
new SocialSecurityNumberValidator(Widget widget, {String allowedFormatChars: "-", String invalidCharError: "You entered invalid characters in your Social Security number.", String wrongFormatError: "The Social Security number must be 9 digits or in the form NNN-NN-NNNN.", String zeroGroupError: "Invalid Social Security number; not allowed all zeros in any digit group.", String wrongAreaError: "Invalid Social Security number; not allowed Area Numbers 666 and 900-999."}) #
Creates a new Object instance.
Object instances have no meaningful state, and are only useful through their identity. An Object instance is equal to itself only.
SocialSecurityNumberValidator(Widget widget, { this.allowedFormatChars:"-", this.invalidCharError:"You entered invalid characters in your Social Security number.", this.wrongFormatError:"The Social Security number must be 9 digits or in the form NNN-NN-NNNN.", this.zeroGroupError:"Invalid Social Security number; not allowed all zeros in any digit group.", this.wrongAreaError:"Invalid Social Security number; not allowed Area Numbers 666 and 900-999." }) : super(widget);
Properties
String allowedFormatChars #
Specifies the set of formatting characters allowed in the input. By default equals "-"
String allowedFormatChars
bool enabled #
Property to enable/disable validation process.
Setting this value to false will stop the validator from performing validation. When a validator is disabled, it dispatches no events, and the validate method returns ValidationEvent.VALID instance.
bool enabled = true
String invalidCharError #
Error message when the value contains characters other than digits and formatting characters defined by the allowedFormatChars property. By default equals "You entered invalid characters in your Social Security number."
String invalidCharError
List<ValidationListener> listeners #
List of ValidationListener's
List<ValidationListener> listeners = new List<ValidationListener>()
OnInvalid onInvalid #
Callback function invokes when result of validation is invalid.
OnInvalid onInvalid
OnValid onValid #
Callback function invokes when result of validation is valid.
OnValid onValid
bool required #
If true specifies that a missing or empty value causes a validation error.
bool required = true
String requiredError #
Error message when a value is missing and the required
property is true.
By default equals "This field is required."
String requiredError = "This field is required."
String wrongAreaError #
Error message when the value contains an invalid Social Security number. By default equals "Invalid Social Security number; not allowed Area Numbers 666 and 900-999."
String wrongAreaError
String wrongFormatError #
Error message when the value is incorrectly formatted. By default equals "The Social Security number must be 9 digits or in the form NNN-NN-NNNN."
String wrongFormatError
String zeroGroupError #
Error message when the value contains an invalid Social Security number. By default equals "Invalid Social Security number; not allowed all zeros in any digit group."
String zeroGroupError
Methods
List<ValidationResult> doValidation(value) #
Executes the validation logic of this validator to validate a Social Security number. For an invalid result returns an List of ValidationResult objects, with one ValidationResult object for each field examined by the validator that failed validation.
You must override this method in a subclass of a validator class.
List<ValidationResult> doValidation(dynamic value) { List<ValidationResult> results = super.doValidation(value); // Return if there are errors or if the required property is set to false // and length is 0. String val = value != null ? value.toString() : ""; if (results.length > 0 || ((val.length == 0) && !required)) return results; else return _validateSocialSecurity(value); }
ValidationEvent validate(value, [bool fireEvent = true]) #
Performs validation of value and notifies the listeners of the result if fireEvent equals true.
ValidationEvent validate(dynamic value, [bool fireEvent = true]) { ValidationEvent resultEvent; if (enabled && (value != null || required)) { // Validate if the target is required or our value is non-null. resultEvent = _processValidation(value); } else { // We assume if value is null and required is false that // validation was successful. resultEvent = new ValidationEvent(ValidationEvent.VALID); } // Send notification to listeners if (fireEvent && resultEvent != null) { listeners.forEach((ValidationListener listener){ listener(resultEvent); }); } // Invoke callback validation functions if (onValid != null && resultEvent.type == ValidationEvent.VALID) { onValid(); } else if (onInvalid != null && resultEvent.type == ValidationEvent.INVALID) { onInvalid(resultEvent.results); } return resultEvent; }