I am not very experienced with regex and I need to validate phone numbers using javascript.
I have a textbox which need to be allowed to accept multiple phone numbers with a delimiter of ';' and the characters that can be allowed for the phone numbers are
Numbers
'+'
'-'
Could someone help me on how I can acheive this using javascript and regex/ regular expressions?
Example:
+91-9743574891;+1-570-456-2233;+66-12324576
I tried the following:
^[0-9-+;]+$
Am not sure if this is correct.
You have placed - in wrong place so, your regex is not working.
Try this(your RegEx, but slightly modified):
^[0-9+;-]+$
or
^[-0-9+;]+$
To include a hyphen within a character class then you must do one of the following:
escape the hyphen and use \-,
place hyphen either at the beginning or at the end of the character class.
As the hyphen is used for specifying a range of characters. So, regex engine understands [0-9-+;]+ match any of the characters between 0 to 9, 9 to +(all characters having decimal code-point 57[char 9] to 43[char +] and it fails) and ;.
To be a bit more restrictive, you could use the following regexp:
/^\+[0-9]+(-[0-9]+)+(;\+[0-9]+(-[0-9]+)+)*$/
What it will match:
+91-9743574891
+1-570-456-2233;+66-12324576
What it won't match:
91-9743574891
+15704562233
6612324576
How about this ^([0-9\-\+]{5,15};?)+$
Explanation:
^ #Match the start of the line
[0-9\-\+] #Allow any digit or a +/- (escaped)
{5,15} #Length restriction of between 5 and 15 (change as needed)
;? #An optional semicolon
+ #Pattern can be repeat once or more
$ #Until the end of the line
Only as restrictive as specified could be tighter, See it working here.
Your regex will match what you allow, but I would be a bit more restrictive:
^\+?[0-9-]+(?:;\+?[0-9-]+)*$
See it here on Regexr
That means match an optional "+" followed by a series of digits and dashes. Then there can be any amount of additional numbers starting with a semicolon, then the same pattern than for the first number.
Related
I am working on a JavaScript regular expression for the following condition check.
Consignment number validation details:
Cnote length :12 Varchar
First Character should be Character Upper Case
Fifth Character may be character or integer
Remaining all integer
Examples of valid strings:
C991S1234567
C30811234567
I have no idea. I have tried a simple regular expression like checking only numbers or alphabets.
I have tried something like this:
^[0-9]
It allows only integers. I do not know how to add constraints to it. Any help will be greatly appreciated.
The regex for this task relies on character classes ([...]) and limiting quantifiers ({n}). ^[0-9] only checks if the first character is a digit.
You can use the following regex:
^[A-Z][0-9]{3}[a-zA-Z0-9][0-9]{7}$
See demo
Explanation:
^ - Beginning of the string
[A-Z] - First is an uppercase English letter
[0-9]{3} - The 2nd, 3rd and 4th characters are digits
[a-zA-Z0-9] - Fifth character is either a letter or digit
[0-9]{7} - Following 7 characters are digits
$ - End of string.
I am trying to write business phone number regex in javascript, my requirements are:
It should contain only digits,dashes and whitespaces
It should not end with - but can end with whitespaces
There should be only 1 - between two groups
It should match numbers with and without - like 1, 123, 678-78
I have tried following regex but it fails for 123-- as it is invalid one anybody please suggest me something
/^([ ]*[0-9]+[-]?[0-9 ]*?([-])[ ]*[0-9]+[ ]*|[0-9 ]*[ ]*)+$/.test('123--2')
Try this
/^[0-9]+(-[0-9\s]+)*$/
I don't know if you still need an answer to this, but this works for your requirements:
/^(?!.+-\s*$)\s*((?:\d+\s*-?\s*)+)$/
Explanation:
^ start of string
(?!.+-\s*$) disallow - (or - followed by whitespace) at the end of the string
\s* optional leading spaces
( start capturing
(?:\d+\s*-?\s*)+ one or more groups of the following:
one or more digits,
possibly followed by whitespace,
possibly followed by a single hyphen,
possibly followed by more whitespace
) stop capturing
$ end of the string
Demo
I am trying to construct a regular expression which accepts alphanumerics only ([a-zA-Z0-9]), except for a single hyphen (-) in the middle of the string, with a minimum of 9 characters and a maximum of 20 characters.
I have verified the following expression, which accepts a hyphen in the middle.
/^[a-zA-Z0-9]+\-?[a-zA-Z0-9]+$/
How can I set the minimum 9 and maximum 20 characters for the above regex? I have already used quantifiers + and ? in the above expression.
How would I apply {9,20} to the above expression? Are there any other suggestions for the expression?
/^[a-zA-Z0-9]+\-?[a-zA-Z0-9]+$/
can be simplified to
/^[a-z0-9]+(?:-[a-z0-9]+)?$/i
since if there is no dash then you don't need to look for more letters after it, and you can use the i flag to match case-insensitively and avoid having to reiterate both lower-case and upper-case letters.
Then split your problem into two cases:
9-20 alpha numerics
10-21 characters, all of which are alpha numerics except one dash
You can check the second using a positive lookahead like
/^(?=.{10,21}$)/i
to check the number of characters without consuming them.
Combining these together gives you
/^(?:[a-z0-9]{9,20}|(?=.{10,21}$)[a-z0-9]+-[a-z0-9]+)$/i
You can do this provided you don't want - to be present exactly in middle
/^(?=[^-]+-?[^-]+$)[a-zA-Z\d-]{9,20}$/
[^-] matches any character that is not -
I am building an application which should accept strings only with the following formats:
12345 (only a number)
12345;23456 (two or more numbers separated by ;)
12345-12367 (a range of numbers separated by a -)
The java script regex should allow only the above formats & shouldn't accept any other formats or symbols . Can anyone come up with a regex for this?
This is the RegExp that you need: /^\d+((;\d+)*|-\d+)?$/
(;\d+)* will check for multiple numbers separated by ";"
-\d+ will check for a range
Try
^[0-9]+([;-][0-9]+)?$
That should work
[0-9]+ matches 1 or more digits
[;-] matches a ; or a -
(...)? is an optional match
^ anchors the start and $ anchors the end of the string
^[0-9-;]{0,50}$
0-9 only accept numbers
-; allow only - and ;
{0,50} allow only 50 chars
Assuming that the number portions you are looking for are 5 digits each time, the following should match what you want.
[0-9]{5}((;|-)[0-9]{5}){0,1}
If you need different lengths, you can update the {5} with either another fixed length or a range such as {3,5} for a string of 3 to 5 digits. If you want to be able to capture more than two numbers with the speperators listed, you can use
[0-9]{5}((;|-)[0-9]{5})*
What can be a regular expression for following type of string
E.g. 1, 2-3, 4..5, <6, <=7, >8, >=9
Here I am using equals, range (-), sequence (..) & greater than/equal to operators for numbers less than 100. These numbers are separated by a comma.
Pls help me in writing a regular expression for this. Thanks in advance.
Atul
How about something like this:
^(\d+(-|\.\.)|[<>]=?)?\d+$
Example using Python:
>>> import re
>>> pattern = '^(\d+(-|\.\.)|[<>]=?)?\d+$'
>>> for s in '1, 2-3, 4..5, <6, <=7, >8, >=9'.split(','):
... print(re.match(pattern, s.strip()).group(0))
...
1
2-3
4..5
<6
<=7
>8
>=9
To be clear, this regex matches only one element in the list. I highly recommend that you preprocess your input by splitting it on commas and trimming the individual elements, like I did in the example above. Even though that's not strictly necessary (you can add this logic to the regex I gave here), it will but quite a bit more efficient and readable.
How the regex works:
Observe that every valid string ends with one or more digits, thus \d+$.
There may or may not be something before that, thus, ^(...)?\d+$.
Those prefixes are either the start of a range, or a comparison:
\d+(-|\.\.) matches a number followed by a dash or two periods.
<=? matches "<" as well as "<=". Likewise for >=?. We can abbreviate this to [<>]=?.
Combining these two options using a pipe (|), which signifies choice, we get
^(\d+(-|\.\.)|[<>]=?)?\d+$
Try this expression:
^(?:\d+(?:(?:\.\.|-)\d+)?|[<>]=?\d+)(?:,\s*\d+(?:(?:\.\.|-)\d+)?|[<>]=?\d+)*$
It consists of the alternation of
\d+(?:(?:\.\.|-)\d+)? for a number followed by an optional expression for a range or sequence, and
[<>]=?\d+ for the inequalities.
That’s repeated in the second parts with a comma and optional whitespace for the list.
And for the condition of only allowing numbers less than 100, you can replace \d+ with [1-9]\d for 1..99 or (?:0|[1-9]\d) for 0..99.
You should totally use a regular expression tool like regex buddy.
You're trying to verify that your string generally looks like the sample?
1, 2-3, 4..5, <6, <=7, >8, >=9
matches
\s*(\d+|\d+-\d+|\d+\.\.\d+|[<>]=?\d+)\s*(,\s*(\d+|\d+-\d+|\d+\.\.\d+|[<>]=?\d+)\s*)*
It's easier to split on , and then match each part with
\s*(\d+|\d+-\d+|\d+\.\.\d+|[<>]=?\d+)\s*
That reads:
white space trimmed, match digits or digits dash digits, or digits dot dot digits, or one of less-than or greater-than with optional equal to digits.
You can compress that down to the harder to read:
\s*((\d+(-|\.\.)|[<>]=?)?\d+)\s*
If you want all your digits to be 1-2 digits only, then change all the \d+ to \d{1,2} or \d\d?