Regex length depending on value and decimal [duplicate] - javascript

This question already has answers here:
jQuery current regular expression
(2 answers)
Closed 1 year ago.
I'm trying to only allow the following numbers in my input:
if the input starts with 0-9 and a decimal follows then max length would be 4
ex. 0.99 or 1.23
if the input starts with 10-99 and a decimal follows then the max length would be 5
ex. 10.12 or 99.99
so far I have this and it works but not as strictly as I'd like
/^[0-9]+(\.[0-9]*)?$/

I believe this is what you are looking for. The {1,2} thing says it can have one or two of the instance before it. The \d specifies a digit.
/^\d{1,2}\.\d{1,2}$/
If you require two decimal places always:
/^\d{1,2}\.\d{2}$/

Try this:
console.log(/^[0-9][0-9]?(\.[0-9]{0,2})$/.test("0.98"));
console.log(/^[0-9][0-9]?(\.[0-9]{0,2})$/.test("0.987"));
console.log(/^[0-9][0-9]?(\.[0-9]{0,2})$/.test("00.98"));
console.log(/^[0-9][0-9]?(\.[0-9]{0,2})$/.test("00.987"));

Related

I need help in creating regex for JavaScript where minimum 10 digit number is allowed and maximum 15. It may optionally have "+" at the start only [duplicate]

This question already has answers here:
Phone number validation with plus sign optional
(2 answers)
Using explicitly numbered repetition instead of question mark, star and plus
(4 answers)
Closed 2 years ago.
I want to create regex for use in JavaScript where minimum 10 digit number is only allowed and maximum 15 digit. It may optionally have "+" at the start only and not at any other places.
You can use the following expression
^\+?\d{10,15}$
See a demo on regex101.com.
I believe this can do the work
var patt = new RegExp("^([+]([0-9]){10,15}$)|^(([0-9]){10,15})$");

I need REGEXP for alpha Numeric zip code, which contains minimum 3 & maximum 10 values [duplicate]

This question already has answers here:
JavaScript regex for alphanumeric string with length of 3-5 chars
(3 answers)
Closed 7 years ago.
I need REGEXP for alpha Numeric zip code, which contains minimum 3 & maximum 10 values.
Invalid inputs are: AAAAA, A1, AA, 12, 2A
Valid inputs are: 123456, 123, A1234, A12, A12A, A9A
This is the regex I'm currently using:
/(^[A-z0-9]\d{3,10})+$/
It doesn't allow to specify only digits like this 12345 but input like A123 matches correctly.
The question is not completely clear. If you mean that you can use between 3 and 10 characters, and these characters can be alphanumerical characters (digits and [A-Za-z]), you can use:
/^(?=.*\d.*)[A-Za-z0-9]{3,10}$/
regex101 demo.
The regex works as follows:
^[A-Za-z0-9]{3,10}$ says the regex consists out of 3 to 10 characters that can be digits and/or A-Z/a-z.
The lookahead (?=.*\d.*) enfoces that the string contains at least one digit somewhere in the string.

How to test if string is 0 or positive integer in JavaScript? [duplicate]

This question already has answers here:
Validate that a string is a positive integer
(16 answers)
Closed 8 years ago.
I would like to allow 0 or positive integers as input. The best solution I saw was
/^[0-9]+$/.test(input)
but this expression will allow the user to use "010", so I changed the expression to
/^(0|[1-9]+[0-9]*)$/.test(input)
Is that correct? Does this expression handle every input?
That expression says
0, or
1-9 followed by 0 or more 0-9
Here's a visualization: Link.
so yes, that should cover all non-negative integers. Whether or not regular expressions is the right tool for the job is debatable.

Regular expression for certain numbers [duplicate]

This question already has answers here:
Matching numbers with regular expressions — only digits and commas
(10 answers)
Closed 9 years ago.
I want to generate a regular expression for the following situation.
a input box should accept numbers and dot in the following format XXX.XX.
It accepts also numbers as XX
It accepts also numbers as X
It accepts also numbers XX.XX
It accepts also numbers X.XX
I did like this: /[0-9]{3}\.[0-9]{2}$/, but it satisfied only point number 1.
Try this:
^[0-9]{1,3}(\.[0-9]{1,2})?$

regex matching decimals in an amount [duplicate]

This question already has answers here:
Regex that matches numeric with up to 2 decimal places
(8 answers)
Closed 9 years ago.
I am new to regex and need to write a regex to match both "100" and "100.00" in an amount value. The last two digits are matched only if these are following a "."
I have tried : ^\d+?(?=([.]{1})\d{2})$ - without any luck
Help is much appreciated
You don't need a lookahead here. You can use an optional non-capture group
^\d+(?:\.\d{2})?$
Use this regex: /^\d+(?:\.\d{2})?$/
Variation on previous answers
^\d{1,8}([\.,]\d{1,2})?$
I've found that users will still enter money values as 1.5 when they mean 1.50, so we use a regex to allow for that as well.
The number of decimal places can be adjusted, as can the number of digits before the decimal point

Categories