Dynamic Validation Using Ng-pattern Angularjs
I am trying to do validation on a input based on value selected from a dropdown. JSFiddle: https://jsfiddle.net/Raj_13290/qqLnqw3f/9/ I am changing the value of ng-pattern in ng-c
Solution 1:
It seems that this is a bug angular.
I found a solution, but it is not very good.
Live example on jsfiddle.
var myApp = angular.module('myApp', []);
myApp.controller("MyCtrl", function($scope) {
var tThis = this;
$scope.dataTypeList = [{
'id': 1,
"label": "Currency"
}, {
'id': 2,
"label": "Number"
}, {
'id': 3,
"label": "Text"
}];
$scope.dataTypeValue;
$scope.textValue = {
text: ""
};
$scope.customPattern = /.*/;
$scope.getCustomPattern = function(pInput) {
if (!$scope.dataTypeValue)
return $scope.customPattern;
var dataTypeId = $scope.dataTypeValue.id;
if (dataTypeId === 1) {
$scope.customPattern = /^\d{1,10}$/;
} elseif (dataTypeId === 2) {
$scope.customPattern = /^\d+$/;
} elseif (dataTypeId === 3) {
$scope.customPattern = /^.*$/;
}
if (!$scope.getCustomPattern.isParser) {
$scope.getCustomPattern.isParser = true;
pInput.$setViewValue(pInput.$viewValue);
} else {
$scope.getCustomPattern.isParser = false;
}
return $scope.customPattern;
};
});
input.ng-invalid {
border-color: red;
}
<scriptsrc="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script><divng-app="myApp"><divng-controller="MyCtrl"><ng-formname="MyForm"><h3>
With dynamic pattern
</h3><selectng-model="dataTypeValue"ng-options="t as t.label for t in dataTypeList"ng-change="getCustomPattern(MyForm.input)"></select><inputtype="text"ng-model="textValue.text"ng-pattern="getCustomPattern(MyForm.input)"ng-model-options="{allowInvalid:true}"name="input"><br><br><br>Data type: {{dataTypeValue}}
<br>Entered value: {{textValue}}
</ng-form></div></div>
Solution 2:
From looking your problem it loks like not updating your CSS class based on the ng-pattern. Not gone into detail but this plunkr might help you.
https://plnkr.co/edit/JSgH3LZHQysIO71u03yF?p=preview
var myApp = angular.module('myApp', []);
myApp.controller("MyCtrl", function ($scope) {
var tThis = this;
$scope.dataTypeList = [{
'id' : 1,
"label" : "Currency"
}, {
'id' : 2,
"label" : "Number"
}, {
'id' : 3,
"label" : "Text"
}
];
$scope.dataTypeValue;
$scope.textValue
$scope.customPattern = '';
$scope.className = "ng-invalid ng-invalid-pattern"$scope.setCustomPattern = function () {
var dataTypeId = $scope.dataTypeValue.id;
console.log(dataTypeId + 'llsdkfalskdf');
if (dataTypeId === 1) {
$scope.customPattern = /^\d{1,10}$/;
} elseif (dataTypeId === 2) {
$scope.customPattern = /^\d+$/;
} elseif (dataTypeId === 3) {
$scope.customPattern = /^.*$/;
}
return$scope.customPattern
};
$scope.$watch("[dataTypeValue, textValue]", function (nw, old) {
var s = $('input[name=input]').val()
$scope.textValue = s;
var pattern = $scope.setCustomPattern()
if (pattern.test($scope.textValue)) {
$scope.className = "ng-valid ng-valid-pattern"
} else {
$scope.className = "ng-invalid ng-invalid-pattern"
}
});
});
Post a Comment for "Dynamic Validation Using Ng-pattern Angularjs"