We help IT Professionals succeed at work.

How do I validate salary form field using reg exp?

Medium Priority
Last Modified: 2008-02-01
Hi all,

    I need to validate a salary field for a form using Javascript, and I can't find a good example of this anywhere on the web.  The salary can be from $0 to, say, $100,000,000, and can include only digits or commas or decimals in the right places.
    I found some old code from another project but it does not work well:
function valsal(obj) {    
     codeToVal = obj.value;
     var pattern =/([0-9]{1,9})[,]*([0-9]{3,3})*[,]*([0-9]{1,3})*[.]*([0-9]{2,2})*/
     var matchArray = pattern.exec(codeToVal)
     if (matchArray)
        return true
          alert('Invalid. Format must be ###.##')
          obj.value = ''
          return false

     The problem with this is that it allows alpha chars ie "xyz44" or "44,000.xyz".  I only half understand the syntax of this as I'm sorta new to Reg Exp but I suspect problem lies in the logic, specifically the use of the "*".  
     If anyone can give me an example of how to do this I would greatly appreciate it.  Thanks!
Watch Question

Use this pattern:


The first caret ^ strictly specifies the start of the string. The last dollar sign $ strictly specifies the end of the string.
Hey, not to be anal, but this is a bit more precise:
The previous solution will let you enter many periods followed many "pairs of numbers", so
11,000...0000 would be okayed.
my solution prevents that.

Good point djsputnik. But you missed the commas. Shorter version:


Keep it Cool!

A better version. The first one will allow the user to have 029,123456789.09...


Try this:
( 1st clause: 1,2 or 3 digits
2nd clause: any number of ",###" )
( just digits )
last clause : either ".##" or nothing

The first clause in the previous solution allowed you to have 11,123,1,... - 1 to 3 digits followed by a comma. You only want 3 digits at a time.

I think eventually we'll nail it :)


Your last suggestion will allow 029,123456789.09 because of the addition of |\d+.

hey etmendz,
notice that the \d+ is "OR"ed with the whole ((\d{1,3})(,\d{3,3})* , not just (,\d{3,3})*. I think I correctly allow for either numbers with ",###"'s or just numbers without commas. If you still think it's incorrect let me know.

Because it is an OR, the 029,123456789.09 will be allowed. Remember, regular expressions do not match against the first true instance in your OR but will rather parse against all possibile matches as defined in your regular expession.

Once the regular expression engine matches the left side of your OR, it does not mean that the right side of your OR will never be evaluated when the engine proceeds reading to the end of the string. This means that the rest of your string may still be matched against the right side of your OR. I am still confident that this makes the best match:


Try this out posconsultant!

If you want to force numbers when the user enters decimal values:


Notice the "+" sign this time.

If you want to require the decimal values (at least 0.00), just remove the last ? before the last $.

Have fun!


Thanks!  This is a big, big help.

Regular expressions...aren't.

You guys are sick, sick, sick ;-)