6

I'm requesting the user give me a nickname that is at least 3 characters and at most 30 characters and only contains letters and numbers.

<form>
    Nick: <input type="text" name="nick" pattern="[A-Za-z0-9]{3,30}">
    <input type="submit" value="Join lobby">
</form>

The problem I'm having is that empty forms are accepted.
As expected, strings that have a length of 1, 2, or 31+, or contain punctuation, do not pass. But if they just don't put anything into the box, it is accepted.

How can I fix this?

1
  • +1. I've changed title - if you don't like the edit fill free to rollback. Commented Jan 4, 2014 at 21:15

1 Answer 1

9

Add the required attribute.

<input type="text" required name="nick" pattern="[A-Za-z0-9]{3,30}" required>

Remember that you still need to do validation on the server side.

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.