Attempting to validate user input in a form before sending it to the server, however, encountering an issue where the input field is not being checked and no error is being reported. Implemented a custom validator directive:
`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;
};
}
};
});`
Corresponding HTML code:
<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>
Struggling to identify the issue or where mistakes may be occurring. Any assistance would be greatly appreciated.