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

2
  • The first one intenger (every) and the second one Integer of Float? Commented May 20, 2016 at 18:53
  • regex101.com/r/fP8qY5/1 Commented May 20, 2016 at 18:59

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

Comments

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");

5 Comments

The link used is from 2010, there are much more modern method now, such as Number.isInteger()
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 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 Number.isInteger() is not cross browser supported, you have to write Polyfill for that.
@Dhaval It's defined in ES6, which is usually good enough. However, according to the MDN it looks like you're right and its support is lagging behind :-/
0

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

5 Comments

You should escape your dot
What are you trying to do with the '?+' at the end? Did you mean to put a '*'?
This works, only can you update it to even accept space please. So if its 123, 123.4 (with a space) it should also pass. Thanks in advance
If you need it to accept spaces you should probably add a couple of \s+. But it would make sense to do what Dhaval said and use String.split(',') and then String.trim() each string.
This will match on 1, 1a - which isn't really valid.
0
^\d+,\s?\d+\.?\d*$

Will be valid to

1234, 1234
123, 123.45

And Invalid To

abd, 123
123, abc
abc, abc

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.