Regular expression in client script to check value between 0 and 100

rahul gupta6
Tera Contributor

Hi,

Need to write a regular expression that allows only numbers between 0 and 100 (including decimals).

 

Used !pattern1=/^[0-9.]*$/ && !pattern2=/^(?:100(?:\.0+)?|[0-9]?[0-9](?:\.[0-9]+)?)$/ but it's not working perfectly.

Any suggestions/solution.

 

 

1 ACCEPTED SOLUTION

Sandeep Rajput
Tera Patron
Tera Patron

@rahul gupta6 Please use the following regular expression instead.

 

/^(100(\.0{1,})?|(\d{1,2})(\.\d{1,})?)$/gm

 

You can test this expression at https://regex101.com/ with several combinations of integer and decimal values.

 

Please mark the response helpful and accepted solution if it manages to address your question. 

View solution in original post

4 REPLIES 4

Sandeep Rajput
Tera Patron
Tera Patron

@rahul gupta6 Please use the following regular expression instead.

 

/^(100(\.0{1,})?|(\d{1,2})(\.\d{1,})?)$/gm

 

You can test this expression at https://regex101.com/ with several combinations of integer and decimal values.

 

Please mark the response helpful and accepted solution if it manages to address your question. 

Used this expression but still there is an issue that if alphabet value is entered in the field, it changes to 0. However, if anything more than 100 is entered, it changes to blank. Need to have it changed to blank in any case if the regex is not matched.

@rahul gupta6 It is not the issue of regular expression but your code. Could you please provide your code here. 

Never mind, changed the field type from decimal to string and it worked. Thank you for your assistance.