Prevent inputting leading 0

mengmeng

I can input

0 but not 0123. How do I prevent users not to input 0 at the first position of the input text field?

Mark Reed

Let them input it, just trim it when focus changes:

inputElement.onblur = function() {
  this.value = this.value.replace(/^0+(?=\d)/,'');
}

Fiddle

Note that it's specifically looking for a digit (\d) after the 0's that it's nuking, so it won't touch the zero in something like 0.123. If you want to get rid of those, too, just change the \d to ., which matches any character whatsoever.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Remove leading column values <= 0

From Dev

Missing leading 0 on binary conversion

From Dev

Mid Function cuts off leading 0

From Dev

php strip leading zeros and 0 decimal values

From Dev

Keep leading 0 in Twig

From Dev

How to prevent inputting 0 in an specific input through jquery and show an error div through jquery?

From Dev

Javascript function argument is inputting as a 0 instead of a 1

From Dev

SSRS Expression To Remove Leading 0 In Date

From Dev

Remove first leading 0 from varchar with MSSQL

From Dev

parsing dates with month without leading 0

From Dev

Prevent user from inputting value larger than int max?

From Dev

Space instead of leading 0 Date Java

From Dev

Regex for allowing numbers without leading and ending 0

From Dev

Add leading 0 with gsub

From Dev

Oracle - Trim and Leading 0

From Dev

why do my elements = 0 after inputting a number into array?

From Dev

Omit leading 0 in char and append

From Dev

Remove leading column values <= 0

From Dev

Javascript function argument is inputting as a 0 instead of a 1

From Dev

Prevent inputting leading 0

From Dev

NSNumberFormatter trucates leading 0's

From Dev

Inputting a character to print error, but not when 0 is input in c

From Dev

Oracle - Trim and Leading 0

From Dev

Prevent Excel truncating leading zeros for HYPERLINKS

From Dev

sql trim some leading 0

From Dev

Prevent macOS double-space from inputting ". " period

From Dev

Leading 0 in pandas dataframe

From Dev

Prevent leading zero in front of the letter grade?

From Dev

Keep leading 0 in date calculation