I am trying to validate a form field in javascript (using bootstrap/jquery) but I need a regular expression which matches a string which is a ma separated list of numbers, there may or may not be spaces.
example: 1,2,3,3.14,6.0, -3.14, -6, 7.13,100
I can get the regex if they are all integers and no spaces in between, but having decimals really plicates things.
I am trying to validate a form field in javascript (using bootstrap/jquery) but I need a regular expression which matches a string which is a ma separated list of numbers, there may or may not be spaces.
example: 1,2,3,3.14,6.0, -3.14, -6, 7.13,100
I can get the regex if they are all integers and no spaces in between, but having decimals really plicates things.
Share Improve this question asked Mar 29, 2016 at 5:49 user4797334user47973344 Answers
Reset to default 6You could try this:
^(\s*-?\d+(\.\d+)?)(\s*,\s*-?\d+(\.\d+)?)*$
document.write(
/^(\s*-?\d+(\.\d+)?)(\s*,\s*-?\d+(\.\d+)?)*$/.test(
'1,2,3,3.14,6.0, -3.14, -6, 7.13,100'
)
);
If you break up the above regex, you'll notice that it is capturing the first number in the string with:
(\s*-?\d+(\.\d+)?)
To capture the first number, it tries matching as many consecutive spaces \s*
as necessary, followed by an optional hyphen (or negative sign) -?
, followed by at least one numeric digit \d+
, followed by an optional decimal point that has at least one consecutive numeric digit following the decimal point (\.\d+)?
.
The next group captures all the numbers following the first one.
(\s*,\s*-?(\d+(\.\d+)?)*
is the same as the previous group except for an additional \s*,
in front allowing for as many spaces as necessary \s*
before matching a ma ,
. This group is repeated as many times as necessary *
.
The regex starts with ^
and ends with $
to make sure it starts matching from the beginning of the string ^
until the end of the string $
.
If regex is not mandatory, then try this simple logic
function isValidInput(str)
{
return str.split(",").filter(function(val){ return isNaN(val) }).length == 0;
}
isValidInput("1,2,3,3.14,6.0, -3.14, -6, 7.13,100");
An alternate to @gurvinder372's answer, you can use Array.every
instead of Array.filter
. Benefit, it will not loop till the end and will break on first invalid entry. But, if you wish to get all incorrect values, then @gurvinder372's answer is preferred.
function isValidInput(str) {
return str.split(",").every(function(val) {
return !isNaN(val);
})
}
var test = isValidInput("1,2,3,3.14,6.0, -3.14, -6, 7.13,100");
alert(test);
You can try this
^(\s*(-|\+)?\d+(?:\.\d+)?\s*,\s*)+(-|\+)?\d+(?:\.\d+)?\s*$
Regex breakdown
^ ---> Starting of string
(\s* ---> Match any number of white spaces
(-|\+)? ---> Match - or + sign (optional)
\d+ ---> Match digits before decimal
(?:\.\d+)? ---> Non capturing group to match digits after decimal(optional)
\s*,\s* ---> Match any number of spaces before and after ma
)+ ---> Do this till last ma (or second last number)
(-|\+)?\d+(?:\.\d+)? ---> Same as above but for checking only the last number as it should not have ma
\s* ---> Check spaces after last number
$ ---> End of string
Demo