1

I have to create input field in Angular, that input field allow only number with 2 decimal places like '123455.12'

Below is what i have tried so far

<input myCustomDirective type="text">

I created Directive. In my CustomDirective I used HostListener for keypress event in that listener i used regular expression to validate but it allow only number it not allowing to enter '.(dot)'

new RegExp('^[0-9]*$');// my regular expression

3
  • AngularJS - Smart Float Directive , This is what are you looking for : gsferreira.com/archive/2015/02/… Commented May 9, 2019 at 19:16
  • i should enter only one '.' but your code i am able to enter more than one dot Commented May 9, 2019 at 19:23
  • Please check out the fiddle jsfiddle.net/gsferreira/Lsv9f0b0 , posted in link , ng-pattern="/^[0-9]+(\.[0-9]{1,2})?$/" is the key Commented May 9, 2019 at 19:38

3 Answers 3

2

Here's a Directive that takes Only Decimal Values with custom precisions.

https://stackblitz.com/edit/angular-decimal-directive

  • It will allow you to put digits before the .(dot)
  • It will not allow you to paste (ctrl+v) non-decimal.
  • It will take single .(dot) . by default it will take 2 digit after the . (dot).

Example:

`<input type="text" [(ngModel)]="myVar" decimal>`

But you can define the precision size like that :-

`<input type="text" [(ngModel)]="myVar" [decimal]="3">`
  • If you want to use Only Integer not floating point you can use the Directive Like this:

    <input type="text" [(ngModel)]="myVar" [decimal]="0">

  • If you don't complete enough precision in the input , then it will automatically complete rest of the number precision (also if a .(dot) needed) by onblur event.

  • You Can't inject string value by two way binding.

N.B: [(ngModel)] is mandatory for using the Directive.

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

4 Comments

If I enterd 99.999 and i select entire text then enter "." this scenario not working.
It will not work, because it will not take double dot(.) together .so if you want to replace whole text with the . (dot) first you have to erase the line with backspace after select entire text.
but we should not force the user. most of the user will select the entire text to override
0

ng-pattern that you reguire is ng-pattern=" /^[.\d]+$/"

Comments

0

Please check out the fiddle jsfiddle.net/gsferreira/Lsv9f0b0 , posted in link , ng-pattern="/^[0-9]+(\.[0-9]{1,2})?$/" is the key

From Plain Old Java script fiddle link https://jsfiddle.net/j3cbytws/

Only numbers please and a single dot allowed , nothing else:

<input type="number" name="someid" onkeypress="return isNumberKey(event)" /> 

<script>
var isDecimalAlredyEcountered = false;
function isNumberKey(evt){
    var charCode = (evt.which) ? evt.which : event.keyCode
    if(isDecimalAlredyEcountered && charCode === 46)
    return false;
   if(charCode == 46)
    {isDecimalAlredyEcountered =true;
    }
    if (charCode > 31 && (charCode < 48 || charCode > 57)&& charCode != 46)
        return false;
    return true;
}    
</script>

There can be an issue of backspace char and delete with the above implementation, so you have to reset the state on clear

1 Comment

thanks for your time. i should enter only one dot but your cases i am able to enter more than one dot

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.