Decimal or numeric values in regular expression validation
A digit in the range 1-9 followed by zero or more other digits:
^[1-9]\d*$
To allow numbers with an optional decimal point followed by digits. A digit in the range 1-9 followed by zero or more other digits then optionally followed by a decimal point followed by at least 1 digit:
^[1-9]\d*(\.\d+)?$
Notes:
The
^
and$
anchor to the start and end basically saying that the whole string must match the pattern()?
matches 0 or 1 of the whole thing between the brackets
Update to handle commas:
In regular expressions .
has a special meaning - match any single character. To match literally a .
in a string you need to escape the .
using \.
This is the meaning of the \.
in the regexp above. So if you want to use comma instead the pattern is simply:
^[1-9]\d*(,\d+)?$
Further update to handle commas and full stops
If you want to allow a . between groups of digits and a , between the integral and the fractional parts then try:
^[1-9]\d{0,2}(\.\d{3})*(,\d+)?$
i.e. this is a digit in the range 1-9 followed by up to 2 other digits then zero or more groups of a full stop followed by 3 digits then optionally your comma and digits as before.
If you want to allow a . anywhere between the digits then try:
^[1-9][\.\d]*(,\d+)?$
i.e. a digit 1-9 followed by zero or more digits or full stops optionally followed by a comma and one or more digits.
Regular expression for the language of Decimals
In the Decimal language they're defining, the fraction is optional. Your regular expression requires the decimal point.
The first alternative (0+D(Z)*)
matches a number without a decimal point. The second alternative ((0+D(Z)*).(0+D(Z)*D))
matches a number with a decimal point.
Regular Expression for negative and positive decimal numbers
Looking at the documentation about RegExp it states that when using the constructor function, the normal string escape rules (preceding special characters with \ when included in a string) are necessary.
Your regex would then match a dot which would match any character making for example a
valid because the preceding [0-9]{0,900}
matches zero - 900 times.
Your regex would become:
var re = new RegExp("^[+-]?[0-9]{0,900}(?:\\.[0-9]{0,900})?$");
To match negative and positive decimal numbers that should not start with a dot, you could use:
^[+-]?[0-9]+(?:\.[0-9]+)?$
Which will match the beginning of the string ^
, an optional [+-]?
, one or more digits [0-9]+
followed by an optional part which will match a dot and one or more digits (?:\.[0-9]+)?
and then the end of the string $
.
var re = new RegExp("^-?[0-9]+(?:\\.[0-9]+)?$");
var re = new RegExp("^[+-]?[0-9]+(?:\\.[0-9]+)?$");var strings = [ "1", "1.2", "0.232", ".232", "-1.2", "+1.2"
];
strings.forEach((s) => { console.log(s + " ==> " + re.test(s));});
javascript regular expression of decimal value with minimum and maximum digits
The regular expression:
/^\d+\.\d{6,15}$/
Should do it.
^
- From beginning of input\d+
- One or more digits [0-9]\.
- Decimal place\d{6,15}
- Between 6 and 15 digits [0-9]$
- To end of input
To test:
var regexp = /^\d+\.\d{6,15}$/;
var test = function (input, result) {
if (regexp.test(input) === result) {
console.log('OK');
} else {
console.error(input, result);
}
}
test('0.0001', false);
test('12310.002301', true);
test('531412.135143613411552', true);
test('531412.1351436134115515', false);
Regular expression for digits, decimals, and fractions
Use
^(?!.*\d+(?:\.\d+){2})\d*\.?\d*\/?\d*\.?\d*[a-z]*$
See proof. This expression disallows three numbers that have a period between one another thanks to (?!.*\d+(?:\.\d+){2})
negative lookahead.
EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
(?: group, but do not capture (2 times):
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
){2} end of grouping
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
\d* digits (0-9) (0 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
\.? '.' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
\d* digits (0-9) (0 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
\/? '/' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
\d* digits (0-9) (0 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
\.? '.' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
\d* digits (0-9) (0 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
[a-z]* any character of: 'a' to 'z' (0 or more
times (matching the most amount possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
Related Topics
System.Text.JSON: How to Specify a Custom Name for an Enum Value
Raise Event in High Resolution Interval/Timer
Word Wrap a String in Multiple Lines
How to Get the Z-Order in Windows
How to Serialize/Deserialize a Dictionary with Custom Keys Using JSON.Net
SQL Error: Incorrect Syntax Near the Keyword 'User'
No Type Inference with Generic Extension Method
Reflection - Getting the Generic Arguments from a System.Type Instance
Entity Framework/Linq Expression Converting from String to Int
Divide Array into an Array of Subsequence Array
Difference Between Wiring Events with and Without "New"
Many-To-Many with Extra Columns Nhibernate
Use the Long Reserved Word as a Variable Name in C#
Linq to SQL: Multiple Joins on Multiple Columns. Is This Possible
How to Represent a Time Only Value in .Net
Where Does Error Cs0433 "Type 'X' Already Exists in Both A.Dll and B.Dll " Come From