0

I have a textarea that can accept only whole and decimal numbers on each new line. I need a JQuery regex that will validate the input on Focus Out. So basically the value on each line should be either an integer or a decimal.

Ex:

Valid Scenarios:

1
51425.125
2552
600000
1.51425
3.65

Invalid Scenarios:

.123
123.
123.  56
1213.56.25
234.   (spaces after the decimal point)

I have tried various regex combinations but none seemed to work perfectly:

\b\s([-+]?(\d+|\.\d+|\d+\.\d*))($|[^+-.])

This regex fails when there are 2 decimal values.

\b[0-9\s]*(\.*[0-9\s]*)*

This regex allows space in between and after values.

\b(?:^|\s)(?=.)((?:0|(?:[1-9](?:\d*|\d{0,2}(?:,\d{3})*)))?(?:\.\d*[1-9])?)(?!\S)

This regex doesn't allow the user to enter multi-line values.

Went through several links but none seems to work for me:
Regular Expression for Decimal or Blank
Regular expression - number with spaces and decimal comma
Decimal number regular expression, where digit after decimal is optional
http://regexone.com/problem/matching_decimal_numbers

Community
  • 1
  • 1
Arthas
  • 421
  • 1
  • 6
  • 23

2 Answers2

0

See this JSFiddle:

/^\d+(?:\.\d+)?$/gm

JavaScript

$("#input").on("input", function(){
    var validateThis = $(this).val();
    var validateCount = validateThis.split("\n").length;
    var result = validateThis.match(/^\d+(?:\.\d+)?$/gm);
    if(result && result.length == validateCount){
        $("#result").text("Valid input! :)");
    } else{
        $("#result").text("Invalid input! :(");
    }
});

HTML

<textarea id="input" rows="4"></textarea>
<div id="result"></div>
Arg0n
  • 8,283
  • 2
  • 21
  • 38
  • Seem to work for given scenarios. [`/^\d+(\.\d+)?$/gm`](https://regex101.com/r/zJ7jR2/1) – Tushar Dec 10 '15 at 14:46
  • This doesn't accept the new line character, and hence throws error. – Arthas Dec 10 '15 at 14:47
  • @Batman Ah, missed that part of the question. Lemme see if i can tweak it. – Arg0n Dec 10 '15 at 14:48
  • @Batman Did you check the above link, Arg0n, use `^\d+(?:\.\d+)?$` to non-capturing groups – Tushar Dec 10 '15 at 14:48
  • @Tushar Any flag to force it to check all to be valid? – Arg0n Dec 10 '15 at 15:04
  • @Arg0n One way could be to count number of lines, `split` by newline and get length of array. Compare with the number of matches, with `gm` flags. If both are equal, all the lines passes the regex. – Tushar Dec 10 '15 at 15:07
  • @Tushar Yeah i thought of that, but was wondering if somehow it could be done in the regex. Anyway, will add to answer. Thanks! – Arg0n Dec 10 '15 at 15:08
  • The RegExs above doesn't seem to work when tried on this site: http://www.regexplanet.com/advanced/dotnet/index.html – Arthas Dec 10 '15 at 15:11
  • When inspected the regex assigned to the textarea, it looks something like this: (?:^\d+(?:.\d+)?(?:\r\n)?$)+ Is this correct? – Arthas Dec 10 '15 at 15:20
  • @Batman Look at the updated answer, i've added a JSFiddle for you to play with. – Arg0n Dec 10 '15 at 15:24
  • You'r basically splitting by \n and then applying the RegEx. But in my case, I cannot use any logics. Just assign the RegEx to the control. So can we include the \n or \r\n in our RegeEx? – Arthas Dec 10 '15 at 16:02
  • You may want to tag the question with e.g. `.NET`, `RegularExpressionValidator` or something then, so we know what the final usage will be. Will remove this answer in 20 minutes. – Arg0n Dec 10 '15 at 16:32
0

So can we include the \n or \r\n in our RegeEx?

Yes, we can. Here's an expression that tests whether the whole text consists of lines with numbers:

^\d+(\.\d+)?(\n\d+(\.\d+)?)*\n?$
  • ^$ - The whole text must match.
  • \d+(\.\d+)? - at least one digit, optionally followed by a period and at least one digit
  • (\n…)* - followed by any number (including zero) of: newline with …
  • \n? - Allow a newline at the end (if you want to).
Armali
  • 18,255
  • 14
  • 57
  • 171