Regex with inputmask for commas - javascript

$("#xyz").inputmask('Regex', {regex: "^[0-9]{1,8}(\\.\\d{1,2})?$"});
Above expression allows me to enter 8 digits and 2 decimals but it doesn't automatically handle commas. Can any one help me with commas.

Your regex is a bit wrong, this should work instead:
^[0-9]{1,8}([,.][0-9]{1,2})$
It will accept up to 8 digits and then a comma or dot, followed by up to another 2 digits.
Edit: This regex requires commas, decimals are optional. Maximum is 999,999,999, minimum is 0.00
^([0-9]{0,3}|0)(,[0-9]{3})?(,[0-9]{3})?(\.[0-9]{1,2})?$

Related

How to write the regexp to accept infinite numbers?

I have one regexp which is finding only 2 digit numbers. I'm trying with \#break:[0-9][0-9]\s\minutes this regexp. It has only 2 digits. How I can rewrite this to detect any number even it 5 or 6 digits.
/\d+/ should do the trick.
"d" is the symbol for digits and "+" tells it to accept one or more.
Try it with d+ as Kevin said: \#break:\d+\s\minute
Or if you exactly know how many digits should be found, just use \#break:\d{1,5}\s\minute, which will catch digits from 1 to 5.

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])$

Indian pincode validation regex - Only six digits, shouldn't start with `0`

I have tried Regex accept numeric only. First character can't be 0 and What is the regex for "Any positive integer, excluding 0" however that didn't work as per my requirements.
I want exact 6 digit numeric number but shouldn't start with 0
I've tried
^[1-9][0-9]{6}*$
^([^0][0-9]){6}$
...
Need fine tuning.
The problem with ^[1-9][0-9]{6}*$ is it is an invalid regex because of {6}* and ^([^0][0-9]){6}$ is that it is allowing any character that is not 0 followed by six digits.
Use
^[1-9][0-9]{5}$
Explanation:
^: Starts with anchor
[1-9]: Matches exactly one digit from 1 to 9
[0-9]{5}: Matches exactly five digits in the inclusive range 0-9
$: Ends with anchor
Regex101 Playground
HTML5 Demo:
input:invalid {
color: red;
}
<input type="text" pattern="[1-9][0-9]{5}" />
This regular expression covers;
PIN code doesn't start from zero
Allows 6 digits only
Allows 6 consecutive digits (e.g. 431602)
Allows 1 space after 3 digits (e.g. 431 602)
([1-9]{1}[0-9]{5}|[1-9]{1}[0-9]{3}\\s[0-9]{3})
Some websites and banks have the habit of spacing pincode after 3 digits.
To match both 515411 and 515 411 the following pattern will help.
^[1-9]{1}[0-9]{2}\s{0,1}[0-9]{3}$
^[1-9]{1} - PIN Code that starts with digits 1-9
[0-9]{2} - Next two digits may range from 0-9
\s{0,1} - Space that can occur once or never
[0-9]{3}$ - Last 3 needs to be digits ranging from 0-9

Regex for validating decimal number with fixed range

I require a regex to validate fixed range decimal numbers e.g. 1234.1234 - valid, 4444.1234 - valid 123.123 - invalid, 1234.123 - invalid
The number 4 digit before decimal and 4 digit after decimal only valid.
I'm currently uses this regex - /^\S((\d{4})((\.\d{4})?))$/ but this not satisfies me.
^\d{4}(\.\d{4})?$
This should do it for you.Use
^[1-9]\d{3}(\.\d{4})?$
If you dont want to match 0234.1234
You can use this regex:
/^\d{4}(?:\.\d{4})?$/
This will match 1234 or 1234.5678 as valid matched.
RegEx Demo

What regex for jQuery satisfies these conditions?

I have a field for which can have:
Up to potentially 2 decimals places
Up to potentially 6 non-decimal places
No more than 8 digits altogether (the max 2 decimals plus max 6 non-decimals)
Be a positive number
So correct inputs would be range from 0.01 to 999999.99, and trailing zeroes isn't an issue, so 4.00 is just as fine as 4.
Try this pattern:
^[0-9]{1,6}(?:\.[0-9]{1,2}0*)?$
If you want to allow leading and trailing whitespace, add \s* to the beginning and the end of the pattern, right after ^ and right before $.
That said, this task is something you might want to accomplish without regex. Why don't you just read the value of the input, parse it and then simply perform a numeric validation? You could even round the input to two decimal places.
You could try something like so:
Up to potentially 2 decimals places: (\.\d{1,2})? - This will match a decimal point followed by a minimum of 1, and a maximum of 2 digits. This is optional.
Up to potentially 6 non-decimal places: \d{1,6} - This will match a minimum of 1 digit and a maximum of 6 digits.
No more than 8 digits altogether (the max 2 decimals plus max 6 non-decimals): You can combing the two above to get this: \d{1,6}(\.\d{1,2})?.
Be a positive number: Change the above to this: ^\d{1,6}(\.\d{1,2})?$. This should make sure that any number you pass to it, does not start with a negative sign. The ^ and $ anchors instruct the regex engine to start matching at the beginning of the string and complete the matching at the end. This should allow you to be sure that the string you are matching is indeed a number.
That being said, you should really be doing numerical range checks using the appropriate mathematical operations which your language (in this case JavaScript) provides. A small change in the numerical range you are after will most likely bring a large change in your regular expression.
This might validate it
# /^(?=.*[1-9].*$)(?=[.]?(?:\d[.]?){1,8}$)(?=\d{0,6}(?:[.]\d{0,2})?$)/
^
(?= # must be a positive number
.* [1-9] .* $
)
(?=
[.]?
(?: # 1 to 8 digits
\d
[.]?
){1,8}
$
)
(?=
\d{0,6} # 0 to 6 non-decimal places
(?:
[.] # 0 to 2 decimal places
\d{0,2}
)?
$
)

Categories