Is there a way to check if the value entered in an input field is already in a specified array? I would like to validate the input field value on key press against an array of names and display an error message if the value is already present in the array. Additionally, I want to disable the button. If the entered value is not in the array, I do not want to display an error message and want to enable the button. You can view the example on Plunkr here.
Here is the HTML code:
<!DOCTYPE html>
<html>
<head>
<script src="angular.min.js"></script>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>
<body ng-app="test" ng-controller="testController">
Name: <input type="text"/>
<button on-keypress="submit()">Add</button>
</body>
</html>
And here is the script:
var testController = angular.module('test', []);
testController.controller('testController', ['$scope', function($scope) {
$scope.names = ["name1","name2","name3"];
angular.forEach($scope.names, function(x) {
console.log(x);
});
}]);