Is it best to use a 'text' attribute with a limit on the number of characters, or can I use a number attribute in an input for a zipcode.
Just trying to get my head around all the different attributes with the forms in html5. Cheers
Is it best to use a 'text' attribute with a limit on the number of characters, or can I use a number attribute in an input for a zipcode.
Just trying to get my head around all the different attributes with the forms in html5. Cheers
“Should” is a strong word, but there is actually a “should” class statement about issues like this. HTML5 CR says about
input type=number
:The means that the only feasible element for the purpose is
input type=text
, when using built-in constructs. You can use themaxlength
attribute to set the maximum number of characters (works on all browsers) and additionally apattern
attribute what specifies the allowed characters and combinations, too; its value naturally depends on the zipcode type(s) to be used. For international postal addresses, you probably should not use anypattern
or evenmaxlength
, since the formats vary.There are various options from my POV, but I think the best is already given by Md Johorul Islam: the
pattern
attribute.The options:
attribute
);type=text
with amaxlength
.Note: Despite these options: always validate server side!