3

I have a input box with angular js validation.. The code is

    <div class="col-sm-12">
        <input type="text" name="drug_name" class="form-control" placeholder="Drug name" ng-pattern="/[aA-zZ\s]$/" ng-model="vmp.medication.drug_name" maxlength="150" typeahead="drugn as drugn.drugName for drugn in vmp.drugList | filter:$viewValue" autocomplete="off"  required>
<span class="pull-right font-10">(Max 150 chars.)</span>
  <span ng-messages="form.drug_name.$dirty && form.drug_name.$error">
   <span ng-message="required" class="error-inner">Drug name is required.</span>
   <span ng-message="pattern" class="error-icn">Only Alphabets are allowed</span> </span>
     </div>

The above code is validating the textbox and throwing error if I enter numbers. But What I need is , It should restrict entering numbers. The user should not be able to enter numbers in the textbox at all.. Can anyone helpme to do this..

2

2 Answers 2

3

Here is what you are looking for ng-pattern-restrict. In your case should looks like:

<input type="text" pattern="[a-zA-Z]+" ng-pattern-restrict />
Sign up to request clarification or add additional context in comments.

Comments

0
app.directive('name', function() {
  return {
    require: 'ngModel',
    link: function (scope, element, attr, ngModelCtrl) {
      function fromUser(text) {
        var transformedInput = text.replace(/[^A-Za-z ]/g, '');
        console.log(transformedInput);
        if(transformedInput !== text) {
            ngModelCtrl.$setViewValue(transformedInput);
            ngModelCtrl.$render();
        }
        return transformedInput;
      }
      ngModelCtrl.$parsers.push(fromUser);
    }
  }; 
});

This won't allow digit input.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.