1

I have wasted a lot of time on this already :(.

I want to accept only the integer or the double-positive numbers: ex: 40000, 500.0000, 400.1234 I managed to do that for the integers:

// somewhere at the code 
export const NUMERIC_PATTREN = '^-?[0-9]\\d*(\\.\\d{1,2})?$';

// component code:
  amount: [{ value: entry.amount, disabled: false }, [Validators.required,Validators.pattern(NUMERIC_PATTREN )]],


However, the input is not accepting values from the format 500.0000 or 400.1234. can anybody help me figure this out please.

Update I want to limit the number of places after the point too.

2
  • Can you create stackblitz? Commented Jul 20, 2019 at 11:53
  • I managed to solve the problem, thanks Commented Jul 20, 2019 at 12:20

2 Answers 2

2

In case you want to accept integer or double with unlimited places after or before the point, this will help:

https://stackblitz.com/edit/angular-g75hxt

amount: [{ value: entry.amount, disabled: false }, [Validators.required, Validators.min(0)]],
Sign up to request clarification or add additional context in comments.

3 Comments

sorry that's not what I needed.
@FurqanS.Mahmoud , but you said that: 'I want to accept only the integer or the double-positive numbers'. It was your requirement.
sorry, I think I wasn't really clear, Yes I need that, plus I need to have only 4 places after the point in case the number is double. that's why I have to use the regular expression for that. min(0) validator is not enough.
1

This Worked for me:

export const NUMERIC_PATTREN = '^-?[0-9]\\d*(\\.\\d{1,4})?$';

this will accept an integer number or a double number with maximum 4 places after the point.

If you want to increase or decrease the places after the point, you need to play with 4, ex:

export const NUMERIC_PATTREN = '^-?[0-9]\\d*(\\.\\d{1,3})?$'; //accepts only 3 places after the point

If you want unlimited places after the point you need to use:

export const NUMERIC_PATTREN = '^-?[0-9]\\d*(\\.\\d*)?$'; //accepts endless places 

If you don't want the user to enter a zero to the left, then you need to start the domain from 1:

^-?[1-9]\\d*(\\.\\d{1,4})?$

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.