简体   繁体   中英

Combine pattern with maxlength of input field

Is it possible to combine a pattern attribute with a maxlength attribute on an input field in jquerymobile?

<input name="aNumber" type="number" pattern="[0-9]{6}" maxlength="6" placeholder="dddddd" value="">
<input name="anotherNumber" type="number" pattern="[0-9]{3,4}" maxlength="4" placeholder="ddd(d)" value="">

I want the big numberpad to appear but that only works using pattern="[0-9]*" . Unfortunately the maxlength attribute is not respected in any case.

What I want is a numeric keypad to appear (the big one/dial; the iPhone apparently has two: only numbers/dial and numbers with special chars) letting the user input up to 6 or 3 to 4 numeric chars.


Edit: Applied @raina77ow 's suggestion and that works for the moment, but still it feels not right so I'm open to advice!

<input name="aNumber" type="tel" pattern="[0-9]*" maxlength="6" placeholder="dddddd" value="">

{6}表示正好6位数{0,6}表示最小0位数,最大6位数

type="tel" pattern="{X,6}" maxlength="6"

if you wanna limit input to strictly numbers (as in math numbers not telephone numbers) go with:

pattern="[0-9]{X,6}"

Where x is an integer number between 0 and 6 will get you the minimum of X and maximum of 6 digits for input.

If you type pattern this way, you need maxlength only for browsers that don't support pattern attribute, I would keep it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM