How could I validate 13 digit phone number with hyphen? - javascript

I have a phone number formatted like this.+251-911-123456.
How could I create a regex that matches the given format.
I was trying to do it with the bellow given expression but it is not a success.
^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$

You may use
/^\+\d{3}-\d{3}-\d{6}$/
Details
^ - start of string
\+ - a + symbol
\d{3} - three digits
- - a hyphen
\d{3} - three digits
- - a hyphen
\d{6} - six digits
$ - end of string.
See the regex demo and the Regulex graph:

Related

Regex for input with numbers and commas

I'm trying to limit input data.
My goal:
only two symbols per input allowed: numbers and a comma
first symbol only number (zero or more)
amount of numbers is unlimited (zero or more)
a dangling comma is allowed but only one
Test cases:
1,2,4 - ок
1221,212,4121212 - ок
,2,3 - not ок
1,2,3, - ок
11,21111,31111, - ок
I've hade something like this but it doesn't work properly
/^\d*(,\d*)*$/.test(value)
Appreciate any help!
You can use
/^(?:\d+(?:,\d+)*,?)?$/
See the regex demo. Details:
^ - start of string
(?:\d+(?:,\d+)*,?)? - an optional non-capturing group:
\d+ - one or more digits
(?:,\d+)* - zero or more sequences of a comma and one or more digits
,? - an optional comma
$ - end of string.

Regular expression for hyphen separated floating point numbers

Need some help in designing regular expression to validate hyphen separated floating point numbers in Javascript. So far I have managed to achieve this RegEx:
(^((\\d)+(\.[0-9]+)?)(\-)?((\\d)+(\.[0-9]+)?)$)|^(\\d+)$
It matches the following:
1) 2
2) 2.10
3) 3.10-3.14
The problem with this one is that its also matching "3.103.310" which is wrong number. Much appreciate any help in fixing this issue.
The problem comes from the first alternative that matches 1 or more digits with an optional fractional part ((\d)+(\.[0-9]+)?) and then matches a hyphen and again 1+ digits and again an optional fractional part. Thus, 2 dots are allowed.
You may fix the pattern like this:
^\d+(?:\.\d+)?(?:-\d+(?:\.\d+)?)*$
See the regex demo
Details
^ - start of string
\d+ - 1+ digits
(?:\.\d+)? - an optional non-capturing group:
\. - a dot
\d+ - 1+ digits
(?:-\d+(?:\.\d+)?)* - an non-capturing group matching 0+ occurrences of
- - a hyphen
\d+(?:\.\d+)? - 1+ digits and 1 or 0 occurrences of . and 1+ digits
$ - end of string

“combine” 2 regex with a logic or?

I have two patterns for javascript:
/^[A-z0-9]{10}$/ - string of exactly length of 10 of alphanumeric symbols.
and
/^\d+$/ - any number of at least length of one.
How to make the expression of OR string of 10 or any number?
var pattern = /^([A-z0-9]{10})|(\d+)$/;
doesn't work by some reason. It passes at lest
pattern.test("123kjhkjhkj33f"); // true
which is not number and not of length of 10 for A-z0-9 string.
Note that your ^([A-z0-9]{10})|(\d+)$ pattern matches 10 chars from the A-z0-9 ranges at the start of the string (the ^ only modifies the ([A-z0-9]{10}) part (the first alternative branch), or (|) 1 or more digits at the end of the stirng with (\d+)$ (the $ only modifies the (\d+) branch pattern.
Also note that the A-z is a typo, [A-z] does not only match ASCII letters.
You need to fix it as follows:
var pattern = /^(?:[A-Za-z0-9]{10}|\d+)$/;
or with the i modifier:
var pattern = /^(?:[a-z0-9]{10}|\d+)$/i;
See the regex demo.
Note that grouping is important here: the (?:...|...) makes the anchors apply to each of them appropriately.
Details
^ - start of string
(?: - a non-capturing alternation group:
[A-Za-z0-9]{10} - 10 alphanumeric chars
| - or
\d+ - 1 or more digits
) - end of the grouping construct
$ - end of string

Generic JavaScript regex validating a positive number with or without commas as thousand separators and an optional fractional part

I have following regex to validate numbers in input
var reg = /^\d+$/;
Now i want to allow ,(commas) and .(period) in number field as following will some one help me writing regex to allow following number format ?
10000000
10,000,000
10000000.00
You may use
/^(?:\d{1,3}(?:,\d{3})+|\d+)(?:\.\d+)?$/
See the regex demo
If you only need to allow 2 digits after the decimal separator, replace (?:\.\d+)? with (?:\.\d{1,2})?.
Details:
^ - start of string
(?:\d{1,3}(?:,\d{3})*|\d+) - 2 alternatives:
\d{1,3}(?:,\d{3})+ - 1 to 3 digits and one or more sequences of a comma and 3 digits
\d+ - 1 or more digits
(?:\.\d+)? - an optional sequence of:
\. - a dot
\d+ - 1 or more digits
$
You could use
^((\d{1,2}(,\d{3})+)|(\d+)(\.\d{2})?)$
see Regex101
or
^((\d{1,2}(,\d{3})+)|(\d+))(\.\d{2})?$
if you want 10,000,000.00 to get matched to.

Regex Min Max for Expression that Allows Spaces

I am using this pattern:
^\w+( \w+)*$
to validate that a string is alphanumeric and may contain spaces. I can't figure out how to set a min and max though. I'd like something like this:
^(\w+( \w+)*){1, 50}$
but it's not working. What is the correct syntax?
EDIT: Sample input:
3this String is fine12
If the length of the entire string is greater than 50 though, it should not match.
If you want to limit the input string length you can use a restrictive positive lookahead at the start:
/^(?=.{1,50}$)\w+(?: \w+)*$/
The input string length can range from 1 to 50 characters.
See regex demo
Explanation:
^ - start of string
(?=.{1,50}$) - the positive lookahead that requires the string to have at least 1 character and up to 50 (note the $ is very important here)
\w+ - 1 or more word characters
(?: \w+)* - zero or more sequences of a space followed by 1 or more word characters
$ - end of string

Categories

Resources