I have a regex to validate the amount entered in a field: /^\d+(\.\d+)?$/
Can I somehow compound the expression such that I can check for the total no.of characters entered? It should allow for a max of 13 characters (with/without decimal)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Sure, just add a lookahead assertion at the start:
^(?=.{0,13}$)makes sure that there are between 0 and 13 characters between start and end of string. It doesn’t actually match and consume any of those characters, so the following part of the regex can then do the validation.Another way would be
Here,
(?!.{14})asserts that it’s impossible to match 14 characters at the start of the string, thereby ensuring a length maximum of 13.Other variations on this theme: