Regular expression for certain numbers [duplicate] - javascript

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})?$

Related

How to allow for dot and comma to be entered in this input field? [duplicate]

This question already has answers here:
Javascript function need allow numbers, dot and comma
(6 answers)
Closed 5 months ago.
The line below restrict the input to be numbers only. How can this be adjusted to accept dot and comma?
Numbers only: <input oninput="this.value=this.value.replace(/(?![0-9])./gmi,'')"></input>
Appreciate your help!
You may replace on the pattern [^0-9.,]+:
<input oninput="this.value=this.value.replace(/[^0-9.,]+/gmi,'')"></input>
Note that technically we should also disallow more than one decimal point, and we also should not allow thousands comma separators in the wrong place.

Regex length depending on value and decimal [duplicate]

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"));

How to define a range from -50 to 50 using regular expression [duplicate]

This question already has answers here:
Using regular expressions to validate a numeric range
(11 answers)
javascript regex optional minus
(1 answer)
Closed 2 years ago.
my question is, how to define a regular expression, if a range is between -50 to 50.
I have used this regular expression:
'\\b(-?[1-9]|-?[1-4][0-9]|-?50|[0-9])\\b'
but it can only check from 0 to 50
Why I use this pattern, because I use this pattern for input field such like this:
<inpit [pattern]="regex">
this pattern can only accept regex string
any solutions??

Regex for allowing Numeric, alphabet, hyphen(-) space and apostrophes(') [duplicate]

This question already has answers here:
REGEX - Allow Numbers and . - /
(7 answers)
Closed 2 years ago.
Currently I have a textbox that accepts only Alphabet, hyphens(-), space and apostrophes('). Now I would like to add numeric values as well to it. Currently I am using the Regex as below:
/^[a-zA-ZÀ-ÖØ-öø-ÿ' -]+$/
How would I achieve adding numerics as well to the above?
To allow numbers, simply add 0-9 in your regex, like so:
/^[0-9a-zA-ZÀ-ÖØ-öø-ÿ' -]+$/

Why is my regular expression for a valid date not working? [duplicate]

This question already has answers here:
Match exact string
(3 answers)
What is the MM/DD/YYYY regular expression and how do I use it in php?
(9 answers)
Closed 4 years ago.
I am trying to learn how to use regular expressions. Currently, I am creating my own regular expression using JavaScript to test for a date in the format of MM-DD-YYYY.
Here is my code:
// regex for testing valid date
var regex = new RegExp("[0-9]{2}\-[0-9]{2}\-[0-9]{4}");
regex.test("113-12-1995");
Unfortunately, this is outputing to true and I cannot figure out why. I am under the impression that {2} means it must be two digits and no more or less. It seems like it is behaving as if I had put a {2,} which would correlate to at least two digits, but that isn't what I want.
Additionally, how would I test to see if the value of the first two digits are greater than 12?

Categories