Dr James Sprinks
Dr James Sprinks

Reputation: 121

Defining a maskRe in javascript

I would like to define a maskRe in javascript that only allows values from 0.002 - 0.1 to be entered in a form, any ideas?

James

On top of this, I would like to create a regex in javascript that only allows values from 0 - 50.00 to be entered in a form, any ideas?

Upvotes: 1

Views: 932

Answers (1)

raina77ow
raina77ow

Reputation: 106483

Please understand that maskRe should be used to filter out invalid symbols - but not to validate value of an input: regex property should be used for this.

You can use both, though:

maskRe: /[0-9.]/,
regex: /^0[.](1|0[1-9][0-9]?|00[2-9])$/

... or ...

regex: /^0[.](1|0[1-9][0-9]*|00[2-9][0-9]*)$/

... if you don't need to limit number of digits after the decimal point to three.

UPDATE: for 0 - 50.00 it would be something like this, perhaps...

regex: /^(50(?:[.]0+)?|[1-4]?[0-9](?:[.][0-9]+)?)$/

Upvotes: 2

Related Questions