0

I am using angularjs and I have an input like

<input type='text' pattern="[0-9.,]+" ng-model="treasuryValue"  />

I set first value to input using ng-model tag. But while setting first value to input my pattern doesn't work. Pattern tag runs first time when input changes. How can I format value in input while setting it's first value.

1 Answer 1

4

Use ng-pattern attribute,

<input type='text' ng-pattern="/[0-9.,]+/" ng-model="treasuryValue" />

To format the value initially,

function formCtrl($scope,$filter){
    $scope.treasuryValue = '33,345';
    $scope.$watch('treasuryValue', function() {
        $scope.name = $filter('number')($scope.name);
  });
}

Demo

Sign up to request clarification or add additional context in comments.

9 Comments

nothing changed. there is no difference between ng-pattern and pattern tags in this case.
Works for me here jsfiddle.net/KGd8K/1049. Im setting a string value which does not work, but if you change it to int/float it will work jsfiddle.net/KGd8K/1050.
Doesn't work. I entered 324234. I expect 324,234. But it returns 324234
The pattern won't do this for you initially. You need to you use filters for that.
You need to add filter in controller. Check this jsfiddle.net/KGd8K/1051
|

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.