Search code examples
htmlpattern-matchinginput-type-file

How to restrict textbox in html to only accept letters with space?


I want to restrict textbox to only accept letters with spaces. I have tried this but it is not accepting spaces

<input type="text" 
       maxlength = "15"  
       name="txtName" 
       pattern="[A-Za-z \s*]"  
       class="form-control"  
       placeholder="Name" 
       required="required" 
       class="validate">

I also try this pattern.

pattern="[A-Za-z][" "]" 

I don't want to use any function for this pattern in my code. How can I do this in simple one line code.?


Solution

  • You are missing the anchors and quantifiers:

    pattern="^[A-Za-z \s*]+$"
    

    here is a working jsfiddle.