forgottofly
forgottofly

Reputation: 2719

To allow input text accept only numbers

Below is my pluner where I have an input text which I need to make it accept only numbers.I have tried by using normal angular Js form validation but the event is not firing up .Has anyone face the similar issue,request to share your thoughts..

Link

  <input type="text" ng-model="valuesForOutputs[item.name][i]" 
               ng-disabled="isDisabled(item.name, i)" ng-pattern="/^(\d)+$/"  
                             required   name="value"
                                               placeholder="Enter value">
       <span class="error pop_up" ng-show="targetForm.value.$error.pattern">Please enter only number</span>

Upvotes: 0

Views: 424

Answers (1)

Ankit Chaudhary
Ankit Chaudhary

Reputation: 4089

You can use onkeypress event of textfield:

 var allowOnlyNumbers=function(field){
        if (!String.fromCharCode(event.keyCode).match('[0-9.]') || (field.value.match('[.]') && String.fromCharCode(event.keyCode) == '.'))
           event.preventDefault();
    };
 <input onkeypress='allowOnlyNumbers(this)' type="text" ng-model="valuesForOutputs[item.name][i]" 
               ng-disabled="isDisabled(item.name, i)" ng-pattern="/^(\d)+$/"  
                             required   name="value"
                                               placeholder="Enter value">

Upvotes: 1

Related Questions