check for length of characters in regex for amount validation

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)


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:

/^(?=.{13})\d+(\.\d+)?$/         # more than 12 characters
/^(?=.{6}$|.{8}$)\d+(\.\d+)?$/   # 6 or 8 characters

Need Your Help

Android JsonObject — I do not know what to do

android json if-statement try-catch android-volley

I can not use the data from the method. I do not know why. Where did the mistake?

MySQL Row-level Revision Control

mysql database table row revision

I’m creating revision control for data in our database. It will have the ability to store revisions, rollback, and rollback rollbacks. The database tables I’m using that needs to be revisioned is b...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.