[英]Angular directive validation and decryption
我正在嘗試構建一個首先驗證用戶輸入的指令。
在驗證輸入后,我想用sha256散列字符串並返回僅包含加密輸入的新模型。
我知道這是一個奇怪的功能,但我有一個客戶需要這樣:)
以下是我在的地方: 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;
}
};
});
通過簡化腳本解決了我的問題。
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);
}
}
}
});
}
};
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.