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
3 Answers
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
-
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.– John3136Commented Jun 21, 2016 at 11:18
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/
-
type="tel"
does not automatically validate telephone number though. See MDN Docs. Regex is what I would suggest to OP too. Commented Mar 19 at 6:54
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>