Search code examples
javascriptangularjsangularjs-directivecryptojs

Angular directive validation and decryption


I am trying to build a directive that first validates the users input.

After the input is validated I want to hash the string with sha256 and return a new model that only contains the crypted input.

I know its a strange feature but i have a customer that need it this way :)

Here is where im at: Plunkr

var app = angular.module("myApp", []);

app.directive('ccInput', function () {

    var types = {
        'socialSecurityNumber': {
            'regex': /^[0-9]{6}-[0-9pPtTfF][0-9]{3}$/,
            'type': 'text',
            'error': 'The value you entered is not a valid social security number.'
        }
    };

    //CryptoJS.SHA256("hejsan Stefan hur måpr du");

    var getType = function (type) {
        return types[type];
    };

    return {
        restrict: 'E',
        require: 'ngModel',
        replace: true,
        scope: {
            ccType: "@",
            ccId: "@",
            ccLabel: "@",
            ccModel: "=ngModel"
        },
        template: '<div class="cc-group"><label class="cc-form-label" for="{{ccId}}">{{ccLabel}}</label><input id="{{ccId}}" class="cc-form-input" data-ng-model="ccModel" type="{{inputType}}" ng-pattern="inputRegex" /></div>',
        link: function (scope, elm, attr, ctrl) {


            var options = {},
                textField = angular.element(elm[0].lastChild),
                parser,
                formatter;
            if (!ctrl) {
                return;
            }


            options = getType(attr.ccType);
            scope.inputType = options.type;
            scope.inputRegex = options.regex;
        }
    };
});

Solution

  • Solved my issue by making the script easier.

    app.directive('valiCrypt', function ($timeout) {
    
        var validateInput =function(input) {
             // Returns validation logic result here.
        }
    
        return {
            restrict: 'A',
            require: 'ngModel',
            replace: true,
            scope: {
                vcModel: "=ngModel",
                vcDuration: "@",
                vcText: "@"
            },
    
            link: function (scope) {
    
                if (!scope.vcDuration) {
                    scope.vcDuration = 0;
                }
    
                if (!scope.vcText) {
                    scope.vcText = "";
                }
    
                scope.$watch('vcModel', function (value) {
                    if (value) {
                        if (value.length === 11) {
                            var result = validateInput(value);
    
                            if (result) {
    
                                var copy = angular.copy(scope.vcModel);
    
                                scope.vcModel = scope.vcText;
    
                                $timeout(function () {
                                    // <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/sha256.js"></script>
                                    copy = CryptoJS.SHA256(value);
                                    scope.vcModel = copy;
                                }, scope.vcDuration, true);
                            }
                        }
                    }
                });
            }
        };
    });