PDA

View Full Version : Regular expression for numeric values



Pravin.Pawar
29 Jan 2013, 12:55 AM
Hi,

I want to apply regular expression over the numeric field which will accept values in %.

I'm using following regexp which is working fine somehow I want to consider % sign after numeric value which user may enter if wants.

I'm using following regex-
regex: /^[0-9]{0,3}(\.[0-9]{1,2})?$/,
regexText: "<b>Error</b></br>Invalid Number entered."

Please let me know how should I consider % symbol after the numeric value. % sign is NOT mandatory somehow if user wish to type then it should be acceptable.

Regards, Pravin

sword-it
29 Jan 2013, 6:11 AM
Hi Praveen,

You can use \d+% for % character, for more info see the below link-

http://groovy.codehaus.org/Documenting+Regular+Expressions+in+Groovy

Pravin.Pawar
29 Jan 2013, 6:26 AM
I used following regular expression and it is working as expected-

regex: /^[0-9]{0,2}(\.[0-9]{1,2})?\s*\%?\s*$/,
regexText: "<b>Error</b></br>Invalid value."

Thanks, Pravin