3

I have an input field of type number. When the user enter multiple zeros (0s) and moves to the next input field, the multiple zeros should go back to single 0.

I have tried the following code in the plunkr: https://plnkr.co/edit/dyCp5ZMKOkZvcsw4F8og?p=preview

<input [(ngModel)]="value" type="number" class="form-control" id="valueId" 
(ngModelChange)="valuechange($event)">

valuechange(newValue) {
//newValue = newValue.toString().replace(/^0+/, '0');
newValue=parseInt(newValue.toString());
console.log(newValue);
}             
1
  • your plunker having different code , Please check and update plunker Commented Apr 20, 2018 at 11:27

1 Answer 1

6

You just need to set 0 as a string when the value is 0, and call function onchange instead. like this

<input [(ngModel)]="value" type="number" class="form-control" id="valueId" 
      (change)="valuechange($event)">


if(this.value === 0){
      this.value = '0';
}

PS: No need to convert using parseInt and toString()

Working Example

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

1 Comment

Glad to know @shweta , if you get what you want please mark it as accepted and upvote it !!

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.