Regex for input with numbers and commas - javascript

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.

Related

Only one minus symbol for negative numbers JS regex

I have this code /^[-0-9\b]+(\.\d{0,2})?$/ for the positive and negative numbers validation
It's working fine for the negative numbers(for ex -12.34), but user can also type a few "-" symbols(ex: -12-3-4.12).
So how can I disable a possibility to type a multiple minus symbols?
You seem to be using the regex for liva validation. In these cases, the regex should comprise only optional parts, those that can match an empty string.
You can use
/^-?\d*\.?\d{0,2}$/
See the regex demo. Details:
^ - start of a string
-? - an optional -
\d* - zero or more digits
\.? - an optional .
\d{0,2} - 0, 1 or 2 digits
$ - end of string.
[-0-9\b]+ Is making it possible to have one or more - characters.
Just change your regex to:
/^-?[0-9\b]+(\.\d{0,2})?$/
Here is the test:
https://regex101.com/r/fbMI7Z/1/

How could I validate 13 digit phone number with hyphen?

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:

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

Regex not working for comma separated list of strings

I need a regex for following values:
Ha2:123hD,Ha2:123hD,Ha2:123hD - correct match - true
Ha2:123hD,Ha2:123hD,Ha2:123hD, - comma on end - false
Ha2:123hD,Ha2:123hD,Ha2:123hD,,Ha2:123hD - double comma- false
,Ha2:123hD,Ha2:123hD,Ha2:123hD - comma at start- false
I am trying the following regex:
/(([a-zA-Z0-9]+)(\W)([a-zA-Z0-9]+))/
/(([a-zA-Z0-9]+)(\W)([a-zA-Z0-9]+,)*([a-zA-Z0-9]+)(\W)([a-zA-Z0-9])+$)/
But it is not working.
You could put the comma at the start of the repeating group.
/^[a-zA-Z0-9]+[:][a-zA-Z0-9]+(?:,[a-zA-Z0-9]+[:][a-zA-Z0-9]+)*$/
If you only need to check for these fix strings (As I have to guess from your question), this will work ^(Ha2:123hD,)*Ha2:123hD$
And otherwise, you just can follow this expression and replace the Ha2:123hD with your wildcard expression.
Also check this website:
https://regex101.com/
It explains nicely how the single symbols of a regex work.
To only match comma-seaprated alphanumeric + : string lists you may use
/^[a-zA-Z0-9:]+(?:,[a-zA-Z0-9:]+)*$/
See the regex demo
Explanation:
^ - start of string anchor
[a-zA-Z0-9]+ - a character class matching 1 or more (due to + quantifier)
ASCII letters or digits or :
(?: - start of a non-capturing group....
, - a comma
[a-zA-Z0-9:]+ - a character class matching 1 or more (due to + quantifier) ASCII letters or digits or :
)* - .... 0 or more occurrences (due to the * quantifier)
$ - 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.

Categories

Resources