regex javascript zipcode password angularjs ionic form validation

Posted: March 17, 2015 in web
angular.module('validator.directive', []).directive('zipcode', function () {
    return {
        require: 'ngModel',
        link: function (scope, elm, attrs, ctrl) {
            var REGEXP = /^(\d{5}-\d{4}|\d{5})$/;
            ctrl.$validators.zipcode = function (modelValue, viewValue) {
                if (ctrl.$isEmpty(modelValue)) {
                    return true;
                }
                if (REGEXP.test(viewValue)) {
                    return true;
                }
                return false;
            };
        }
    };
}).directive('password', function () {
    return {
        require: 'ngModel',
        link: function (scope, elm, attrs, ctrl) {
            var REGEXP = /[a-zA-Z0-9]{7,}/;
            ctrl.$validators.password = function (modelValue, viewValue) {
                if (ctrl.$isEmpty(modelValue)) {
                    return true;
                }
                if (REGEXP.test(viewValue)) {
                    return true;
                }
                return false;
            };
        }
    };
});

usage:

Should be a valid Zip Code

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s