I am trying to validate input to a form before passing it to the server but it doesn't seem to check the input field, and doesn't report an error. my directive code to create a custom validator:
`var QUERY_REGEXP = /[A-Z,a-z,0-9]{1,20}/;
app.directive('q', function() {
return {
require: 'ngModel',
link: function(scope, elm, attrs, ctrl) {
ctrl.$validators.q = function(modelValue, viewValue) {
if (ctrl.$isEmpty(modelValue)) {
return true;
}
if (QUERY_REGEXP.test(viewValue)) {
return true;
}
return false;
};
}
};
});`
html for the same:
<div ng-controller="CreditsController">
<div style="padding-top:20px" >
<form name='form' novalidate>
<b>Enter Name: </b><input id="creditq" ng-model='query1' name='query1' type="text" q />
<button id="Submit" ng-click='click()' ng-value='search' required>Search</button><br/><br/>
<span ng-show="form.query1.$error.query1">The value is not valid!</span>
</form>
</div>
I can't figure out what I'm missing or doing wrong. Any help would be appreciated.