2

I am creating a form in html and for my first and last name text inputs, I want to have a regex validation on them that only allows the user to input: letters of the alphabet, spaces, and hyphens.

I have tried this pattern but to no avail:

[A-Za-z -]

My HTML code:

<input type="text" id="first_name" name="first_name" maxlength="25" required="required" pattern="[A-Za-z -]"/>

I know how to get only alphabet characters, but allowing spaces and hyphens only is something I'm unable to manage.

1 Answer 1

8

Add anchors and a quantifier:

^[A-Za-z -]+$
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.