I want to create a regex that does the following
<\numberMAX8>[space or not]<\symbol(-)>[space or not]<\numberMAX8> and max 10 times of all of this - I don't care about end spaces, also numbers must be between 5-8.
To explain it a bit more I'll give a few examples
ex:
5-6 7-6 8-8 6-7 ok
4-7 not ok //because of 4
7 - 6 ok
7-6-6-6 not ok because of the - in the middle
Below is what I have so far without having included the mid spaces.
^([5-8](?:-|\s)[5-8][\s]){1,10}
-> <-//didnt work.
Here you go:
^([5-8]\s*-\s*[5-8]\s*){1,10}$
So the explanation is:
The regex matches a starting number from 5-8 ^[5-8], then an arbitrary number of spaces \s*, then dash -, then arbitrary number of spaces \s*, then a number from 5 to 8 [5-8], then an arbitrary number of spaces \s*, and that pattern from 1 to 10 times {1,10}, and nothing after the pattern $.
Related
I need a regex that allows UP TO 4 digit number AND/OR 4 decimal places.
ONLY allowed format examples (I'm just using only 1s to make the format looks simple, it all should be [0-9])
1
1.1
1.11
1.111
1.1111
11
11.1
11.11
11.111
11.1111
111
111.1
111.11
111.111
111.1111
1111
1111.1
1111.11
1111.111
1111.1111
So far the closest one I've gotten --
"^[0-9]{0,4}(?:.[0-9]{0,4}$)"
It works pretty good when I enter numbers + decimals.
However, if I just enter numbers without decimals, I can enter 4+ number digits, which it should not.
The ^[0-9]{0,4}(?:.[0-9]{0,4}$) only seems to be almost working for you. As . is unescaped, it matches any char including digits, and you have an impression it works.
Once you properly escape . with a literal \, it will stop "working", because this will require a dot.
You need to use
/^\d{0,4}(?:\.\d{0,4})?$/
"^[0-9]{0,4}(?:[.][0-9]{0,4})?$"
Details:
^ - start of string
[0-9]{0,4} / \d{4} - zero to four digits
(?:\.[0-9]{0,4})? - an optional occurrence of a . and then zero to four digits
$ - end of string.
Am trying to validate a mobile number 254777123456 against a regex /^((254|255)[0-9]+){9,15}$/, the mobile number should be prefixed with the country codes specified but the total length of the mobile number should not be more than 15 characters, doing this via javascript am getting null, can anyone point out what am doing wrong.
PS. Am using way more country codes than the ones I specified, I just put those two as a test before I add the others because they will all be separated by the pipe.
Your regex ^((254|255)[0-9]+){9,15}$ means, that pick at least 4 digits (of which first 3 should be either 254 or 255) and whole of them must occur at least 9 times to max 15 times, which will mean the minimum length of string that will match should be of 36 characters. Which obviously you don't want. Your regex needs little correction where you need to take [0-9] part out and have {9,12} quantifier separately. Correct regex to be used should be this,
^(?:(?:254|255)[0-9]{9,12})$
This regex will match 254 or 255 separately and will restrict remaining number to match from 9 to 12 (as you want max number to be matched of length 15 where 3 numbers we have already separated out)
Demo
var nums = ['254777123456','255777123456','255777123456123','2557771234561231']
for (n of nums) {
console.log(n + " --> " + /^(?:(?:254|255)[0-9]{9,12})$/g.test(n));
}
This question already has answers here:
RegExp range of number (1 to 36)
(8 answers)
Closed 6 years ago.
I'm looking for a way to validate a phone number with the length of 7, and within the rage of 8600000–9999999 (This is a region in New Zealand, witch is why google was no help as no one talks about New Zealand -- ever). Does anyone know a regEx that can do this?
I'm new to JS, but I think that a Regular Expression would be the best way to do this. (If not let me know.)
Note: The input data type of the phone number is text. (I know I can use number but I ran into bugs with it.)
EDIT -- no lines / separation, just numbers. (Also very fast answers just wow)
Thanks in advance !
This one's actually pretty easy:
/8[6-9][0-9]{5}|9[0-9]{6}/
As long as you don't need separators, anyways.
It's also pretty simple to follow:
8 matches the literal character 8.
[6-9] matches any single character that is in the ASCII range between the characters 6 and 9 (inclusive). This means that it will match any single 6, 7, 8, or 9.
Likewise, [0-9] matches any single character that is in the ASCII range betwen the characters 0 and 9. This is synonymous with "any single digit".
{5} means "match the preceding token 5 times". In this case, it's applied to [0-9], meaning it'll match 5 digits in a row.
The | (pipe) character in a regex is an alternation - it means "match either the pattern on the right or the left". This is how the regex handles the two different cases - 8600000-8999999 is handled by the pattern on the left, while 9000000-9999999 is handled by the pattern on the right.
You can use the regular expression /(?:9[0-9]|8[6-9])[0-9]{5}/.
You can click to play with it on regex101.com, and see some test cases.
Here's the breakdown (provided by regex101.com):
(?:9[0-9]|8[6-9]) Non-capturing group
1st Alternative: 9[0-9], left of |
9 matches the character 9 literally
[0-9] match a single character present in the list below
0-9 a single character in the range between 0 and 9
2nd Alternative: 8[6-9], right of |
8 matches the character 8 literally
[6-9] match a single character present in the list below
6-9 a single character in the range between 6 and 9
[0-9]{5} match a single character present in the list below
0-9 a single character in the range between 0 and 9
{5} exactly 5 times
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])$
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