Regular Expression for Double and Integer Validation

Regular Expression for double and integer validation

You could use an alternation that matches either 3 digits [1-9][0-9]{2} or match the regex you tried with the optional part that matches the dot and a single digit.

^(?:[1-9][0-9]{2}|[1-9][0-9]?(?:\.[0-9])?)$

Regex demo

Explanation

  • ^ Assert start of the string
  • (?: Non capturing group

    • [1-9][0-9]{2} Match a digit 1-9 followed by 2 digits 0-9
    • | Or
    • [1-9][0-9]? Match a digit 1-9 followed by an optional digit
    • (?:\.[0-9])? Optional non capturing group that matches a dot and a digit 0-9
  • ) close non capturing group
  • $ Assert the end of the string

RegEx for double and integer in JavaScript

Is this what you want? Optional minus, followed by an integer, followed by a dot or coma, followed by another integer:

/^-?[0-9]+([.,][0-9]+)?$/

Regular expression for double number range validation

I guess this is the most accurate:

/^(0\.[1-9]|[1-9][0-9]{0,2}(\.[1-9])?)$/

Note that you don't need the RegExp constructor when working with regex literals:

 re = /^(0\.[1-9]|[1-9][0-9]{0,2}(\.[1-9])?)$/
a = [0, 0.1, 123, 123.4, '00.1', 123.45, 123456, 'foo']
a.map(function(x) { console.log(x, re.test(x)) })

0 false
0.1 true
123 true
123.4 true
00.1 false
123.45 false
123456 false
foo false

Regexp for a double

There's nothing wrong with the regex per se, it's your escaping that's at fault. You need to double escape the \ character since that's also a C++ string escape character.

Additionaly there is an edge case where this regex would think that 1. is a valid floating pointer number. So you might be better off with /^[0-9]+(\\.[0-9]+)?$ which eliminates that possibility.

Regex for integer or double values

This should works for you:

@"-?\d+(?:\.\d+)?"

Matchs only the dot only when have digits after it.

Regular expression for double values using QRegExp

This is what I'm using as a RegEx String for positive/negative float values

[+-]?\\d*\\.?\\d+

Regular expression for any double type number

It looks like you also want to match something like .3, right? But you have to be sure your regex doesn't match a decimal point by itself .. So you could do it with these alternations:

myVar = q.match(/\d+\.\d*|\.?\d+/);

\d+\.\d* matches 3., 3.3, 3.33 etc.

\.?\d+ matches .3, .33, 3, 33, etc.

ALTERNATE:
If you need to allow commas for thousands, millions, etc., use the following:

myVar = q.match(/\d{1,3}(,\d{3})*\.\d*|\d{1,3}(,\d{3})*|\.\d+/);

\d{1,3}(,\d{3})*\.\d* matches 3., 3.3, 3.33, 3,333.3 etc.

\d{1,3}(,\d{3})* matches 3, 33, 3,333 etc.

\.\d+ matches .3, .33, etc.

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.

Javascript Regex: validating a double/float

If "any number given will be less than 24", so that doesn't need to be separately tested for, then the following expression will work.

^\d{0,2}(\.\d{0,2}){0,1}$

See http://rubular.com/r/YDfHr5T5sQ

Tested against:

23.75             pass
1.4 pass
1 pass
0.5 pass
0 pass
.2 pass
1.897 fail
%#$#@$# fail
Words fail
other characters fail

Explanation:

^             start matching at the start of the string
\d{0,2} look for zero to two digits
( ){0,1}$ look for this next thing zero or one time, then the end of the string
\.\d{0,2} match exactly one decimal followed by up to two digits

Note - this regex does match the "empty string". You might want to test for that separately if there's a chance that will somehow make its way to this expression...

Simple code to test in your javascript:

var str = "12.345";
var m = str.match(/^\d{0,2}(?:\.\d{0,2}){0,1}$/);
var goodTime;
if (!m) {
alert(str + " is not a good time");
}
else {
goodTime = m[0];
alert("found a good time: " + goodTime);
}

Note - I made a tweak to the regex - adding ?: in the "bit after the decimal" group. This just means "match but don't capture" so the result will return only the match m[0] and not the group .34 in m[1]. It doesn't actually matter since I assign goodTime the value in m[0] (but only if it's a good time). You can see this in action here



Related Topics



Leave a reply



Submit