Clark Bernales
Clark Bernales

Reputation: 53

How to make the input type="number" to not accept number with leading zeros?

This is my current code for the input field. How can I make it display a message saying that the number I've entered should not start with zero? thanks

<input type="number" min="10" max="1000" class="form-control" name="payment" 
placeholder="enter payment" style="text-align:center" autofocus required/>
<br/>

For instance, typing 020 should not be accepted. Only numbers between 10 to 1000.

Upvotes: 5

Views: 5193

Answers (2)

MaanooAk
MaanooAk

Reputation: 2468

This should work:

<input type="text" class="form-control" name="payment" 
 placeholder="enter payment" style="text-align:center" autofocus required 
 pattern="[1-9]\d*" title="A number with no starting zeros"/>

https://jsfiddle.net/spL2par2/

Upvotes: 3

b0ne
b0ne

Reputation: 655

try this using HTML's pattern attribute

<input type="number" class="form-control" name="payment" 
placeholder="enter payment" style="text-align:center" pattern="[1-9]\d*" autofocus required/>
<br/>

Upvotes: 0

Related Questions