1

I want to validate entry in textbox using regex. The pattern should be

integer, integer or float

so valid entries can be

1234, 1234
123, 123.45

and invalid entries will be

abd, 123
123, abc
abc, abc

so far I have tried

var entry = "123,123.4" 
var patt = new RegExp("^[0-9]+,(?:[\d+(\.\d+]+,?){0,1}$");
res = patt.test(entry);  

but it returns false, though it should return true

Garima Jain
  • 17
  • 1
  • 5

5 Answers5

4

Seems like you mixed up the second part of your regex a bit. ^\d+,\s*\d+(?:\.\d+)?$ should be what you need.

Here is a Regex101 sample.

  • \d+ matches one or more digits (an integer)
  • ,\s* matches comma, optionally followed by spaces
  • \d+(?:\.\d+)? matches one or more digits, optionally followed by a dot and one or more digits (an integer or float)

Note1: This will not match negative numbers. You could adjust your regex to ^-?\d+,\s*-?\d+(?:\.\d+)?$ if this is required

Note2: This will not match .1 as a float, which might be a valid float. You could adjust your regex to ^\d+,\s*(?:\d+|\d*\.\d+)$ if this is required.

Sebastian Proske
  • 8,255
  • 2
  • 28
  • 37
2

First split string by ","

var entry = "123,123.4";
var entryArray = entry.split(',');

then just check entryArray items using following function I got from link

function isFloat(n) {
    return n === +n && n !== (n|0);
}

function isInteger(n) {
    return n === +n && n === (n|0);
}

And here is full recipe

function isFloat(n) {
    return n === +n && n !== (n|0);
}

function isInteger(n) {
    return n === +n && n === (n|0);
}

function isValidInput(input){
    if (!input) {return false};
    var inputArray = input.split(',');
    inputArray = inputArray.map(function(item){
        return item.trim();
    })
    inputArray = inputArray.filter(function(n){
        var number = parseFloat(n);
        return number && (isInteger(parseFloat(n)) || isFloat(parseFloat(n)) )
    })
    return (inputArray.length === 2)
}
isValidInput("55,55.44");
Community
  • 1
  • 1
Dhaval
  • 389
  • 1
  • 10
  • The link used is from 2010, there are much more modern method now, such as [Number.isInteger()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger) – Aaron May 20 '16 at 18:57
  • 1
    There's a million ways to verify whether a number is an int or a float, that doesn't make this method less valid. – Andrew May 20 '16 at 19:00
  • 1
    @Andrew It's not less valid but it might be less efficient, and anyway it seems wasteful to write code when there's a native function that already does the same thing. – Aaron May 21 '16 at 10:06
  • @Aaron Number.isInteger() is not cross browser supported, you have to write Polyfill for that. – Dhaval May 21 '16 at 18:01
  • @Dhaval It's [defined in ES6](http://www.ecma-international.org/ecma-262/6.0/#sec-number.isinteger), which is usually good enough. However, according to the [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger#Browser_compatibility) it looks like you're right and its support is lagging behind :-/ – Aaron May 21 '16 at 18:15
1

Replace you regExp by this one:

var reg = new RegExp("^[0-9]+,[0-9]+.?[0-9]*");

I think this is what you want.

To accept space after comma:

var reg = new RegExp("^[0-9]+, [0-9]+.?[0-9]*");

For case of 1a it's needed to ad $ in the end

Samvel Petrosov
  • 7,580
  • 2
  • 22
  • 46
0
^\d+,\s?\d+\.?\d*$

Will be valid to

1234, 1234
123, 123.45

And Invalid To

abd, 123
123, abc
abc, abc
scel.pi
  • 703
  • 6
  • 9
0

Try this one:

^(?:\d{1,3})(?:\.\d{1,2})?$

652.16 valid

.12 invalid

511.22 valid

1.23 valid

12.3 valid

12.34 valid

4b0
  • 21,981
  • 30
  • 95
  • 142
Abhishek Gautam
  • 1,617
  • 3
  • 18
  • 29