A limit with one or two numeric digits Regex - javascript

I am trying to figure out how to create a regex that will let me search for not only one or double digits, but also have a limit from 1-60. I want double digits to be applicable as well so for example, 01-09 works just as well as 1-9.
^([1,2,3,4,5]\d{0,1}|(60))
is what I have so far in terms of setting up the the double digits, but I then can't get 7, 8, 9 to pass as either single or double digits. Any idea on how to solve this problem or do I need to do a case by case approach?

try this;
^0*([1-9]$|^[1-5][0-9]$|^60$)
from this generator
ie, can either be just 1-9, or 1-5 followed by 1-9, or 60 itself.
Edit amended to force begining and end of string and allows leading zeros

Related

Implemention of regex for float and int Bangla Numbers using js

I'm facing a problem.
I want to validate Bangla int and float numbers using RegEx.
I am using the following RegEx
([০-৯][০-৯]*$)
But it's not working on ২৬৭
And again, How I would implement floating bangla numbers (For example:৬৭.৫৬) supported regex once which will select int and float Bangla Numbers. Please somebody help me. (It's for my learning purposes only)
You may use the following to match simple integer or float Bengali numbers:
/[০-৯]+(?:\.[০-৯]+)?/
/[\u09E6-\u09EF]+(?:\.[\u09E6-\u09EF]+)?/
You may validate them (the whole string should match) with
/^[০-৯]+(?:\.[০-৯]+)?$/
/^[\u09E6-\u09EF]+(?:\.[\u09E6-\u09EF]+)?$/
See the regex demo.
Details:
^ - start of string
[\u09E6-\u09EF]+ - one or more Bengali digits
(?:\.[\u09E6-\u09EF]+)? - an optional occurrence of a . and one or more Bengali digits
$ - end of string.
Try this:
regex = /[০-৯]+(\.[০-৯]*)?$/
console.log(regex.test("২৬৭"))
Maybe that was a little bit confusing. Let's break the regex /[০-৯]+(\.[০-৯]*)?$/ down.
First part - `[০-৯]+`
This means that it will match 0-9, one or greater number of times.
Second part - `\.[০-৯]*`
This means that it will match a decimal point ., along with zero or greater numbers.
Third part - `(\.[০-৯]*)?`
The brackets just mean that you are grouping it. The question mark makes it optional.
Fourth part - `$`
Matches to the end of the string.
Putting that together and summarizing: First it matches for a string of numbers. After it encounters a non-number, it checks if it is a decimal point. If it is, then it matches the decimal, and however many numbers come after it (which might be none, meaning that the decimal is there for significant figure purposes, or for another reason).

Javascript Regex for amount field(not allow digits other then zero after decimal point)

I am doing amount validation and i want to write a regular expression in the below format,
1) It should not allow digits but zero(0) can allow after decimal point (i.e, not allow numbers like 23.33, 12.24 but allow 13.00)
2) Decimal point not mandatory for the amount field
I tried using the following code but its taking digits after decimal point,
/^(?!0)\d+(\.\d{1,2})?$/)
Forget about regex, all you need is check if :
function is_valid(num) {
return num == (num|0)
}
console.log(is_valid(24.33))
console.log(is_valid(23.00))
console.log(is_valid(12))
console.log(is_valid('12.67'))
console.log(is_valid('12.00'))
A better method for ECMA Script 2015 or later: Number.isInteger(num) (IE not supported) (I have to admit
that it may be slower, though.)
Besides num | 0 which floors a number, sometimes I will also use ~~num. There are many other methods like Math.floor(num), though. See here for more information.
But if you really like a regular expression (not suggested): /^\s*[+-]?\d+(\.0*)?\s*$/
Remove \s* to disallow spaces.
Change [+-] to - to disallow positive signs.
Change 0* to 0{0,2} or 0{1,2} to allow only up to 2 d.p.
Visit here to design and check your own regular expression.
Document for Number.isInteger
Actually you should use <input type="number"> instead. (IE 10+)

Regular Expression: Help Matching a number less than 24

so I'm making this regular expression to verify some text boxes on a website that I'm designing for an internship.
The problem is that I'm not so keen on regular expressions, and I'm close to having a working one that matches a number between 0-24 and no more than two decimal places.
This is what I have so far. The pattern is also matching any string; such as, "a" or "az".
var pattern = "^([0-9]{0,2}?.?[0-9]{0,2}|1[0-9].?[0-9]{0,2}|2[0-4].?[0-9]{0,2})$";
To get a number between 0 and 24 (24 excluded) with optional up to two decimal places:
^(\d|1\d|2[0-3])(\.\d{1,2})?$
The decimal part:
\. - match the decimal dot
\d{1,2} - one or two digits
()? - makes it optional
The whole part:
\d - numbers 0-9
1\d - numbers 10-19
2[0-3] - numbers 20-23
(x|y|z) - one of x, y or z
As for the "why is my version matching things like "a" and "az" part" - it's a little complex, but it basically boils down to you using dots (like .?). In regex, a dot means "any one character". To make it match a literal dot, you need to escape it with a slash just like I did.
Minor remark: If you want optional leading zero for single digit numbers, replace 1\d with [01]\d. If you want mandatory leading zero for single digit numbers, replace \d|1\d with [01]\d. If you don't want leading zeroes, leave it as it is.
Assuming you do not want 05 or 5.50
^((?:[0-9]|1[0-9]|2[0-3])(?:\.(?:[1-9]|[0-9][1-9]))?)$
You can try it here
The following is a quick attempt to match a floating point number from 0 to 24.99 with up to two non-zero digits
^(([0-9])|([01][0-9])|(2[0-4]))(\.[0-9]{1,2})?$
I think it might be easier to use math to do this though...
You can see the explanation of the entire regex as well as test it out here. I have also added a few test cases.
^(\d|[01]\d|2[0-3])(\.\d{1,2})?$
Test cases:
Valid:
22
1.29
2.99
9.99
13.24
17.38
20.01
02.15
15.35
23.56
1.1
Invalid:
24.29
235.215
21.256
To get a integer number between 1 and 23: ^([1-9]|1[0-9]|2[0-3])$

Regex for number with decimals and thousand separator

I need regex to validate a number that could contain thousand separators or decimals using javascript.
Max value being 9,999,999.99
Min value 0.01
Other valid values:
11,111
11.1
1,111.11
INVALID values:
1111
1111,11
,111
111,
I've searched all over with no joy.
/^\d{1,3}(,\d{3})*(\.\d+)?$/
About the minimum and maximum values... Well, I wouldn't do it with a regex, but you can add lookaheads at the beginning:
/^(?!0+\.00)(?=.{1,9}(\.|$))\d{1,3}(,\d{3})*(\.\d+)?$/
Note: this allows 0,999.00, so you may want to change it to:
/^(?!0+\.00)(?=.{1,9}(\.|$))(?!0(?!\.))\d{1,3}(,\d{3})*(\.\d+)?$/
which would not allow a leading 0.
Edit:
Tests: http://jsfiddle.net/pKsYq/2/
((\d){1,3})+([,][\d]{3})*([.](\d)*)?
It worked on a few, but I'm still learning regex as well.
The logic should be 1-3 digits 0-1 times, 1 comma followed by 3 digits any number of times, and a single . followed by any number of digits 0-1 times
First, I want to point out that if you own the form the data is coming from, the best way to restrict the input is to use the proper form elements (aka, number field)
<input type="number" name="size" min="0.01" max="9,999,999.99" step="0.01">
Whether "," can be entered will be based on the browser, but the browser will always give you the value as an actual number. (Remember that all form data must be validated/sanitized server side as well. Never trust the client)
Second, I'd like to expand on the other answers to a more robust (platform independent)/modifiable regex.
You should surround the regex with ^ and $ to make sure you are matching against the whole number, not just a subset of it. ex ^<my_regex>$
The right side of the decimal is optional, so we can put it in an optional group (<regex>)?
Matching a literal period and than any chain of numbers is simply \.\d+
If you want to insist the last number after the decimal isn't a 0, you can use [1-9] for "a non-zero number" so \.\d+[1-9]
For the left side of the decimal, the leading number will be non-zero, or the number is zero. So ([1-9]<rest-of-number-regex>|0)
The first group of numbers will be 1-3 digits so [1-9]\d{0,2}
After that, we have to add digits in 3s so (,\d{3})*
Remember ? means optional, so to make the , optional is just (,?\d{3})*
Putting it all together
^([1-9]\d{0,2}(,?\d{3})*|0)(\.\d+[1-9])?$
Tezra's formula fails for '1.' or '1.0'. For my purposes, I allow leading and trailing zeros, as well as a leading + or - sign, like so:
^[-+]?((\d{1,3}(,\d{3})*)|(\d*))(\.|\.\d*)?$
In a recent project we needed to alter this version in order to meet international requirements.
This is what we used: ^-?(\d{1,3}(?<tt>\.|\,| ))((\d{3}\k<tt>)*(\d{3}(?!\k<tt>)[\.|\,]))?\d*$
Creating a named group (?<tt>\.|\,| ) allowed us to use the negative look ahead (?!\k<tt>)[\.|\,]) later to ensure the thousands separator and the decimal point are in fact different.
I have used below regrex for following retrictions -
^(?!0|\.00)[0-9]+(,\d{3})*(.[0-9]{0,2})$
Not allow 0 and .00.
','(thousand seperator) after 3 digits.
'.' (decimal upto 2 decimal places).

Reg Expression Javascript for Millions with limit

I am looking to create a regular expression in javascript that does the following:
Allows for 1 or more numbers
Then has an optional period (".")
Then has an optional number of digits up to 6
The context is that i need people to enter in numeric values in the millions and i want them to at least include a 0 if they are entering thousands... so they could enter the following:
1 (would be one million)
0.725 (would be 725k)
10.5 (would be 10M 500K)
I also need to ensure that the value doesn't reach over 725.00 (or 725 million).
Thanks in advance.
That sounds like:
/^(?!\d{4})(?![89]\d\d)(?!7[3-9]\d)(?!72[6-9])(?!725\.0*[1-9])(0|[1-9]\d*)(\.\d{1,6})$/
which means:
doesn't start with four digits (i.e., is less than 1000)
doesn't start with 8 or 9 followed by two digits (i.e., is less than 800)
doesn't start with 73-79 followed by a digit (i.e., is less than 730)
doesn't start with 726-729 (i.e., is less than 726)
doesn't start with 725. followed by zero or more zeroes followed by a nonzero digit (i.e., is less than or equal to 725.00).
starts either with 0, or with 1-9 followed by zero or more digits
after that, optionally a decimal point followed by between one and six digits
That said, I'd actually recommend implementing the above as several separate checks, rather than cramming it all into one regex like the above. In particular, the "is less than or equal to 725.00" check is probably better implemented using numeric comparison; and even if you do want to use a regex for that, you probably want to detect it as a separate error from 0.1asefawe so you can give a more precise error-message.
So basically you want a number that would be multiplied by 10^6 to get the true value.
This sounds like a two-stepper; First, verify that the input string is in a format you expect (you can use a regex for this very easily). Then, parse the string into a number variable and test the actual value. The regex pattern for that would look like "[0-9]{1,3}(\.[0-9]{1,6})?", basically matching a number with up to 3 whole digits and 6 fractional digits, the decimal place and fractional digits being optional. If it matches this pattern, then it's parsable into a number, and you can then perform a quick check that your number <= 725.
I honestly don't think it's feasible to create a single Regex that can validate a proper numeric format AND an inclusive maximum range, but here's a start:
"^(725(\.0{1,6})|(([7][2][0-4]|[7][0-1][0-9]|[1-6][0-9]{2}|[1-9][0-9]|[0-9])(\.[0-9]{1,6})?)$"
This will allow any natural whole number from zero to 724, with any fractional part up to six digits from ".000001" to ".999999". It does this in stages; it will match 720-724, or 700-719, or any three-digit number up to 699, or any two-digit number, or any one-digit number. Then, it will also match the quantity "725" explicitly, with an optional decimal point and up to 6 zeroes.
EDIT: While your comment states that you used this pattern, and it does produce the correct result, I had intended it as a "what not to do"; this pattern will be far more costly to evaluate than the first solution, just to avoid a server-side rule check. And you will have to perform a server-side validation anyway; anything done within the confines of the user's browser should be suspect because the user can disable JavaScript or can even use browser plug-ins like FireBug to make your HTML page behave the way he wants, instead of the way you designed it.

Categories