2

Is it possible to make some kind of pattern matching with html5 to only allow 3-digit numbers to a text form. I do not want the input type to be number because I dont like the visual design of it.

The closest I get is pattern=".{0.3}" But that accepts all kind of text input - not limited to numbers.

<input type="text" name="price" id="price" pattern="fancy code here">

Question: Can you make a pattern in input type="text" that accepts only integers in the max length of 3?

8
  • 1
    If pattern accepts regexes, how about just pattern="\d{3}"? Commented Nov 28, 2013 at 17:02
  • Great, pattern="\d{0,3}" was what I was looking for. Thanks! Commented Nov 28, 2013 at 17:04
  • My regex does not allow empty strings, hence the {3} criteria. Your regex accepts digits of length 0, 1, 2 or 3. Commented Nov 28, 2013 at 17:06
  • Use \d{1,3} to accept digits of length 1-3. Commented Nov 28, 2013 at 17:13
  • Yeah but if i leave it empty as null I can still send that data. I want like a not null check within the regex. Its a form that sends to a database. Commented Nov 28, 2013 at 17:14

1 Answer 1

1

Use this pattern:

\d{1,3}

and the required attribute

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.