I want a very simple Regex for Javascript validation of phone number, which allows 10 digits and checks min numbers should be 10 and max 12 including - dash two times for ex. 123-123-1234
I have found some on internet, but none of them is working for min / max length.
Looking forward for quick response here.
Thanks !
You could do this
/^(?!.*-.*-.*-)(?=(?:\d{8,10}$)|(?:(?=.{9,11}$)[^-]*-[^-]*$)|(?:(?=.{10,12}$)[^-]*-[^-]*-[^-]*$) )[\d-]+$/
See it here on Regexr
(?!...) is a negative lookahead assertion
(?=...) is a positive lookahead assertion
^ # Start of the string
(?!.*-.*-.*-) # Fails if there are more than 2 dashes
(?=(?:\d{8,10}$) # if only digits to the end, then length 8 to 10
|(?:(?=.{9,11}$)[^-]*-[^-]*$) # if one dash, then length from 9 to 11
|(?:(?=.{10,12}$)
[^-]*-[^-]*-[^-]*$ # if two dashes, then length from 10 to 12
)
)
[\d-]+ # match digits and dashes (your rules are done by the assertions)
$ # the end of the string
What you asking for wouldn't be a simple regular expression and also may be solved without any use 'em.
function isPhoneNumberValid(number){
var parts, len = (
parts = /^\d[\d-]+\d$/g.test(number) && number.split('-'),
parts.length==3 && parts.join('').length
);
return (len>=10 && len<=12)
}
For sure this may be a bit slower than using a compiled regex, but overhead is way minor if you not going to check hundreds of thousandths phone numbers this way.
This isn't perfect in any way but may fit your needs, beware however that this allow two dashes in any place excluding start and end of number, so this will return true for string like 111--123123.
There's no simple way to do this with regexp, especially if you allow dashes to appear at some different points.
If you allow dashes only at places as in your example, then it would be ^\d{3}-?\d{3}-?\d{4}$
This one: ^[\d-]{10,12}$ matches string with length from 10 to 12 and contains only digits and dashes, but it also will match e.g. -1234567890-.
Related
I have a couple of regex which I am planning to combine.
So the first regex is as below (allows amounts with particular thousand and decimal separators)
"^-?(\\d+|\\d{1,3}(,\\d{3})*)?(\\.(\\d+)?)?$"
I have similar other regexes (based on different locales e.g. other one would have comma as the decimal separator)
So with the above regex, following are Valid/Invalid values
123.11 (Valid)
1'23 (Invalid)
With the second regex, I want that the string can contain a max of 13 digits (including before or after the decimal)
^[^\\d]*?(\\d|\\d[^\\d]+){0,13}$
With the above regex, following are Valid/Invalid values
1234567890123 (Valid - 13 digits)
12345678901234 (Invalid - 14 digits)
1234567890.123 (Valid as 13 digits...10.3)
1234567890.1234 (Invalid as 14 digits...10.4)
Is it possible to somehow consolidate the 2 regex?
However, I do not want to touch the first regex (have different combinations based on different locales). But it would be nice to somehow dynamically append the 2nd regex into the first one ?
So, I am flexible with the 2nd regex as that is not based on any locale, but is going to be the same always and mainly validates for max of 13 digits in the string.
I'll then validate my string using the consolidated regex.
You may keep the first pattern as is, and just prepend it with
(?=^\D*(?:\d\D*){0,13}$)
The (?=^\D*(?:\d\D*){0,13}$) pattern represents a positive lookahead that matches a location that is immediately followed with
^ - start of string
\D* - 0+ non-digits
(?:\d\D*){0,13} - 0 to 13 occurrences of a digit followed with a non-digit char
$ - end of string.
Full JavaScript regex definition:
var regex1 = "^-?(\\d+|\\d{1,3}(,\\d{3})*)?(\\.(\\d+)?)?$"; // Not to be touched
var consolidated_regex = "(?=^\\D*(?:\\d\\D*){0,13}$)" + regex1;
See full regex demo.
Details
My requirement is to validate a NON-ZERO number. The regular expression that I used is the following.
^[1-9]?\d+(\.\d)?\d*$
VALID VALUES should be
2
22
2222 (up to any number of digits)
2.2222 (up to any number of decimal points)
INVALID VALUES
0
(. without decimal values)
0.1 (any number of decimals, but start digit is 0)
2.4.5 (more than one .)
basically any values starting with 0 or has more than one . or no decimal points provided when . is added, are INCORRECT.
^[1-9]\d*(?:\.\d+)?$
https://regex101.com/r/wHZUoW/1
-Since you don't want the number to start with 0, you shouldn't make the [1-9] at the beginning optional with ?.
-As general good practice, a non-capturing group (?: ... ) is used instead of a capturing group, because the contents do not need to be referenced later.
You can use this regex to validate your numbers:
^(?=[1-9])\d*\.?\d+$
It uses a regex proposed by #WiktorStribiżew as a comment to this question to match decimal numbers (^\d*\.?\d+$), and adds a positive lookahead to ensure that the first character of the number is not 0. Note that if you want to allow numbers such as .3, you should add . to the lookahead character class i.e. ^(?=[1-9.])\d*\.?\d+$
Demo on regex101
[1-9]+[0-9]*(\.[0-9]+)? should work for your case.
[1-9]+ will make sure that the expression starts with a number different than 0.
[0-9]* will make sure to allow the expression to have zeros after the first digit.
(\.[0-9]+)? will allow an extension to the expression, which has to have a . and at least 1 number after it. The ? in the end makes it optional.
By the way, I really like this website to test my regular expressions: https://regexr.com/, you should try it.
I have the following specifications for a regex:
-> The string starts with a string of three numbers
-> It is followed by a '-'
-> That is followed by three uppercase vowels
-> That is followed by a '-'
-> That is followed by three numbers
-> That is followed by a final '-'
-> That is followed by the last three uppercase vowels.
-> Second set of numbers can not equal the first.
-> The second group of letters can not equal the first.
-> The groups of numbers may not contain zero.
A passable string is:
368-IOU-789-AIO.
An invalid string is:
368-AEO-368-AEI
354-AOU-431-AOU
Currently, I have something like this:
([0-9]+[0-9]+[0-9]+[/AEIOU/]+[0-9]+[0-9]+[0-9])
What you have won't work since + means "one or more of". For example, the sequence [0-9]+[0-9]+[0-9]+ will match anywhere between three and an infinite number of digits.
In addition, your current attempt:
allows for one to an infinite number of vowels (and possibly / character);
doesn't require a vowel set at the end;
doesn't require the - separators; and
may allow for arbitrary content before and after the match.
You should be able to use the {count} specifier to get an exact quantity. All but one of those limitations can be done with any basic regex engine, with something like:
^[1-9]{3}-[AEIOU]{3}-[1-9]{3}-[AEIOU]{3}$
The ^ and $ anchors means start and end of string, [1-9]{3} gives you exactly three non-zero digits, [AEIOU]{3} gives you exactly three vowels, and - gives you the literal separator character.
The "groups cannot be identical" rule is a little more problematic. I would just post process for that to ensure it's not violated. The following pseudo-code is what I mean:
def isValid(str):
if not str.regex_match("^[1-9]{3}-[AEIOU]{3}-[1-9]{3}-[AEIOU]{3}$"):
return false
return str[0..2] != str[8..10]
and str[4..6] != str[12..14]
The alternative will be a rather complex regex that future developers will probably curse you for inflicting on them :-)
Note that your "The groups of numbers may not contain zero" is a little ambiguous in that it may mean no zeros are allowed or just 000 is not allowed. I've assumed the former but it's easy adjustable to cater for the latter:
def isValid(str):
if not str.regex_match("^[0-9]{3}-[AEIOU]{3}-[0-9]{3}-[AEIOU]{3}$"):
return false
return str[0..2] != str[8..10]
and str[4..6] != str[12..14]
and str[0..2] != "000"
and str[8..10] != "000"
You can use capture group and backreferences
^([0-9]{3})-([AEIOU]{3})-(?:(?!\1)[0-9]){3}-(?:(?!\2)[AEIOU]){3}$
Regex Demo
The groups of numbers may not contain zero. From this if you meant only digits between 1 to 9 then you can replace [0-9] with [1-9]
If you don't want to have 000 then you can add a negative lookahead ^(?!.*000) to avoid matching 000
I need to extract certain part of Javascript string. I was thinking to do it with regex, but couldn't come up with one which does it correctly.
String can have variable length & can contain all possible characters in all possible combinations.
What I need to extract from it, is 10 adjacent characters, that match one of next two possible combinations:
9 numbers & 1 letter "X" (capital letter "X", not X as variable letter!)
10 numbers
So, if input string is this: "[1X,!?X22;87654321X9]ddee", it should return only "87654321X9".
I hope I've explained it good enough. Thanks in advance!
This Regex will work:
\d{9}X|\d{8}X\d|\d{7}X\d{2}|\d{6}X\d{3}|\d{5}X\d{4}|\d{4}X\d{5}|\d{3}X\d{6}|\d{2}X\d{7}|\d{1}X\d{8}|\d{10}|X\d{9}
As described, It need to match 9 digits and any letter, and the letter can be at any position of the sequence.
\d{9}X # will match 9 digits and a letter in the end
\d{8}X\d # will match 8 digits a lettter then a digit again
...
\d{1}X\d{8} # will match 1 digits a lettter then 8 digits
\{10} # will match 10 digits
Edited to match only X
You can use this much simpler regex:
/(?!\d*X\d*X)[\dX]{10}/
RegEx Breakup:
(?!\d*X\d*X) # negative lookahead to fail the match if there are 2 X ahead
[\dX]{10} # match a digit or X 10 times
Since more than one X is not allowed due to use of negative lookahead, this regex will only allow either 10 digits or ekse 9 digits and a single X.
RegEx Demo
This regex has few advantages over the other answer:
Much simpler regex that is easier to read and maintain
Takes less than half steps to complete which can be substantial difference on larger text.
I want to create a RegExp validation in JavaScript for string which can have 3 parts:
P1-P2-P3
With following rules:
P1, P2 and P3 can only be digits
each part can be 1-13 digits long
entire string can not be longer then 20 characters
P2 and P3 parts are optional (meaning 34 is already a valid string, or 34-6565 or, 566-233455-23232).
Currently I have this, but I am missing the entire string length and I don't know how to define optional parts:
/^.\d{1,13}-\d{1,13}-\d{1,13}$/
Here are few valid entries: 5656, 33434-2323-45, 12-4345-12, 1234567890123-123456, 1234567890123-12-56
Invalid entries: 34453454351234566787, 1234567890123-1234567890123, 23455-233-123-3434, 34sd1322-23, 31234as, ...
You can use:
^(?=\d{1,13}(-\d{1,13}){0,2}$)[\d-]{1,20}$
Online Demo: http://regex101.com/r/sM6wQ7
Explanation:
Try this:
/(^\d{1,13}(-\d{1,13})?(-\d{1,13})?$){1,20}/
? means 0 or 1 of this, so this is useful for optional parts.
{1,20} at the end of the whole string to check it wont be longer than 20.
/^(?!.{21,})\d{1,13}(-\d{1,13}){0,2}$/
(?!.{21,}) at the beginning - negative lookahead meaning: not more than 20 characters.
The total string length check must be separated; the rest can be done with regular expressions:
var re = /^\d{1,13}(?:\-\d{1,13}){0,2}$/;
if (str.length <= 20 && str.match(re)) {
// valid
}
Try this one :
/^(?=.{0,20}$)\d{1,13}(-\d{1,13}){0,2}$/
Description :
^ beginning of the input
(?=.{0,20}$) followed by any char, 0 or 20 times, + end of the input
\d{1,13} a digit, 1 or 13 times
(-\d{1,13}){0,2} a dash + same as above, 0 or 2 times
$ end of the input
More on regular expressions : http://www.javascriptkit.com/javatutors/redev.shtml.
(\d{1,13}-){0,2}(\d{1,13})
Matches two sets of numbers of up to 13 digits of length followed by a dash, then a set of numbers of up to 13 digits of length. This approach is extensible, because you just need to change the number after the first capturing group to change the amount of number parts you want. I would recommend you checking via code if the string's length is appropriate before validating trough Regex. It will be faster and more readable.
Example: http://regex101.com/r/wI7uX1
You could also do this via Javascript, and it may be quite faster: http://jsfiddle.net/sauYY/