1

Trying to write a javascript regex for forcing users to enter decimal after a whole number, doesn't seem like many people want to do this as my searches have yielded nothing that suits my needs.

Match: 2.00 20.00 200.00 1.73 0.10

No Match: .2 .20 . 1 10 1.0 0.1 1.

Here is what I have currently:

var regexp=/^[0-9]{1,}\.{1}[0-9]{2}$/;

In plain english, users must enter money in X.XX format. Just a whole number by itself is disallowed.

I'd appreciate any help and also teaching insight anyone has to offer in making this work.

EDIT:

Here is code:

var notempty=/[a-zA-Z0-9\.\$]/g;
var money=/^[0-9]{1,}\.{1}[0-9]{2}$/;

if ( (notempty.test(document.getElementById('numbers').value)) && (!money.test(document.getElementById('numbers').value)) )
{alert('Wrong format');}
Bob
  • 165
  • 2
  • 13
  • 4
    How about `/^\d+\.\d{2}$/`? - http://regexr.com/3cak4 – Josh Crozier Dec 01 '15 at 01:45
  • @JoshCrozier Just note that would also allow `0003.01` which might not be what OP wants. – Derek 朕會功夫 Dec 01 '15 at 01:47
  • That's the same thing I already have, just written differently, and no, it doesn't work. "1" returns true, although "10" does not. "1" would need to be written as "1.00" in order to match. – Bob Dec 01 '15 at 01:47
  • @Bob with your regex "1" is returning false to me. – rcorreia Dec 01 '15 at 01:49
  • 3
    How does your approach not work? Give some examples, because all of the `No Match` items don't match and all of the `Match` items do match. – Spencer Wieczorek Dec 01 '15 at 01:49
  • I'm running a double condition check on a form field the first checks to see if the field contains anything at all and the second is the above regex to check for monetary formatting. – Bob Dec 01 '15 at 01:51
  • Have you tried to test the regex alone? Use your browser's console. – rcorreia Dec 01 '15 at 01:52
  • Ok, so I just removed the 1st condition, `notempty` and just checked for the monetary formatting, and you guys are right, it does work. Any idea why it's not working with a double condition check? – Bob Dec 01 '15 at 01:58

1 Answers1

3

Your initial regular expression is correct, the problem is actually your notempty check can short-circuit the condition if the value is false. For example if you have a character which is not a letter, number, dollar sign, or period the /[a-zA-Z0-9\.\$]/ it will evaluate false and ignore the item after the && as the values don't matter because false && [any conditions] = false. For example if the input value was "+":

if ( false && ... )
    // invalid format [the expression was false, so this isn't ran]

Since the first condition is false the entire expression becomes false, leading to a improper "correct format". The "empty check" isn't needed in this case anyway, you can simply have:

if (money.test(document.getElementById('numbers').value) {
    // valid format
} else {
    // invalid format
}

As noted by @JostCrozier, your expression can be simplified to: /^\d+\.\d{2}$/, which is functionally equivalent.

Spencer Wieczorek
  • 21,229
  • 7
  • 44
  • 54