0

I have a form in HTML5 and I want to add validation elements to the name, address and phone fields. So far I have the following code:

name='name' pattern='(-.''[A-Za-z])'
name='phone' pattern='[0-9+()x-' ']'
name='address' pattern='[0-9A-Za-z][.,#/\:;''&*]'

For the name, I need it to be able to accept the -'. symbols.
For the phone, I need it to be able to accept +()0-9 x and space.
For address, I need it to be able to accept all of the characters I've put there.

I'm not sure I've done it right though, as when testing I don't get error messages.

2
  • Just a friendly reminder : Never trust the user. Never forget back-end values verification. Commented Jul 13, 2017 at 1:23
  • 2
    It appears you're not very familiar with regex. I highly recommend learning using a tutorial, but there's also many pre-made regexes already available online, such as ones on this site. Commented Jul 13, 2017 at 1:27

1 Answer 1

3

You need to enclose the pattern in " as you are also using ' within your pattern itself.

Sample for the name field, including the correct pattern string:

<form>
<input type="text" name="name" required pattern="[-.'A-Za-z]+" /><input type="submit">
</form>

(assuming you want to allow -'. plus the characters, your requirements are not 100% clear to me)

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

1 Comment

Thanks Marvin. Great help...that fixed the issues :)

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.