Jordan
Jordan

Reputation: 580

How to past in text box only 7 digits

I have an HTML form in a web app for android. one of the fields is a text box that getting auto numbers from the location of the device. but the problem is that I need only 7 numbers and it's pasting much more, how can I restrict this field with js and it's will paste only the first 7 numbers? I tried to do max length and js valid code, but nothing worked, it's still pasting a lot of numbers. Thanks, Jordan

Upvotes: 0

Views: 469

Answers (3)

Konstantin Dinev
Konstantin Dinev

Reputation: 34895

You can specify the size attribute to be 7. The below example works even if the type is different from "text".

<input type="text" size="7" />

Upvotes: 1

frshca
frshca

Reputation: 1339

Could you paste the HTML code for you input? The maxlength attribute should definitely work.

it should look like:

<input type="number" maxlength="7" size="7">

*note - the size attribute alone won't actually limit the amount of characters your input can accept.

Upvotes: 3

Alexander
Alexander

Reputation: 23537

Actually, the attribute you are looking for is maxlength.

<input type="text" maxlength="7" />

maxlength [HTML5]

If the value of the type attribute is text, email,search, password, tel, or url, this attribute specifies the maximum number of characters (in Unicode code points) that the user can enter; for other control types, it is ignored. It can exceed the value of the size attribute. If it is not specified, the user can enter an unlimited number of characters. Specifying a negative number results in the default behavior; that is, the user can enter an unlimited number of characters. The constraint is evaluated only when the value of the attribute has been changed.

Upvotes: 0

Related Questions