14

I am new to HTML,can any one please help me to validate a phone number only with '+' and numeric values and phone number shud not exceed 13 numbers,and to validate email with a '@' and a '.',I dont want to use javascript

0

3 Answers 3

37

In HTML5 you can use <input type='tel'> and <input type='email'>

You can also specify a specific pattern like <input type='tel' pattern='[\+]\d{2}[\(]\d{2}[\)]\d{4}[\-]\d{4}' title='Phone Number (Format: +99(99)9999-9999)'>

Something like pattern='^\+?\d{0,13}' Would give you an optional + and up to 13 digits

2
  • can we add two kinds of pattern for the same field,ie user can either enter a phone number with a '+' or a number? Commented Jun 21, 2016 at 9:11
  • You can modify the pattern regex to make the + optional. Plenty of hits on google for it. If the answer helped please upvote or accept.
    – John3136
    Commented Jun 21, 2016 at 11:18
0

Email Validation - <input type="email" />, use the type as email.

Telephone number validation - <input type="tel" />. use the type as tel.

Reference:

https://www.sitepoint.com/client-side-form-validation-html5/

HTML5 phone number validation with pattern

HTML5 Email Validation

1
  • type="tel" does not automatically validate telephone number though. See MDN Docs. Regex is what I would suggest to OP too.
    – henrylin03
    Commented Mar 19 at 6:54
0

HTML and/or JavaScript will only validate email on client side, the more secure solution would be to validate email on the server side. Having said that you can do basic validation in HTML 5 like

<form>
<input type="email" placeholder="Your email" required>
<input type="submit" value="Submit">
</form>

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.