Skip to content Skip to sidebar Skip to footer

How To Accepts Only Character Values In Html5

Is there any simple way I can use to prevent user from accepting numeric values in html textbox? I've encountered some features of HTML5 like type='email' etc... Now is there any

Solution 1:

I don't know how well supported it is but the pattern attribute should allow you to do this.

<input name="test"type="text" pattern="[A-Za-z]+">

Solution 2:

I would do like that with jQuery :

JQuery

$("#only-text").on('keyup', function(e) {
    var val = $(this).val();
   if (val.match(/[^a-zA-Z]/g)) {
       $(this).val(val.replace(/[^a-zA-Z]/g, ''));
   }
});

See the working fiddle.

Solution 3:

If you want to restrict characters that can be typed into your inputs, you will have to use some Javascript to do so, example with jQuery can be found here

With plain JS you could do something like

document.getElementById("alphaonly").onkeypress=function(e){ 
var e=window.event || e 
var keyunicode=e.charCode || e.keyCodereturn (keyunicode>=65 && keyunicode<=122 || keyunicode==8 || keyunicode==32)? true : false 
} 

where "alphaonly" is id of your input

Post a Comment for "How To Accepts Only Character Values In Html5"