Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 Validate Input to accept alphabets only

Tags:

html

input

I have an input firstname

<input pattern="[a-zA-Z]" oninvalid="setCustomValidity('Please enter on alphabets only. ')" type="text" class="form-control" name="first_name" placeholder="Firstname">

I enter it as : qwerqwe

I kept getting

Please enter on alphabets only. print out

enter image description here

Isn't qwerqwe suppose to be valid ? I'm a little confuse now, and not sure what I missed here.

Can someone please provide me some hints on this ?

like image 729
code-8 Avatar asked May 31 '16 14:05

code-8


2 Answers

You are allowing only ONE character. You missed the + sign:

<input pattern="[a-zA-Z]+" oninvalid="setCustomValidity('Please enter on alphabets only. ')" type="text" class="form-control" name="first_name" placeholder="Firstname">

As @le_m said in comments, you can write * wildcard to allow empty input:

<input pattern="[a-zA-Z]*" oninvalid="setCustomValidity('Please enter on alphabets only. ')" type="text" class="form-control" name="first_name" placeholder="Firstname">
like image 187
Marcos Pérez Gude Avatar answered Oct 22 '22 22:10

Marcos Pérez Gude


use title attribute for validation name.

pattern="[A-Za-z0-9].{1,}" title="Please Enter Valid Company Name"
like image 21
Shrishailam Shelke Avatar answered Oct 22 '22 22:10

Shrishailam Shelke