Jquery Do Not Allow Alphabets to Be Entered in Input Field

jQuery do not allow alphabets to be entered in input field

Replace

var value = $(this).val();

by

var value = String.fromCharCode(e.which) || e.key;

After all, you need to check which key has been pressed before allowing a character to be typed into the field.

Also, make sure the backspace and delete buttons and arrow keys aren’t blocked!

$(function() {  var regExp = /[a-z]/i;  $('#test').on('keydown keyup', function(e) {    var value = String.fromCharCode(e.which) || e.key;
// No letters if (regExp.test(value)) { e.preventDefault(); return false; } });});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="test" name="test" />

jQuery disable letters in input forms

As of jQuery v1.7, "live" is deprecated. You should use "on" for event delegation. Ex:

<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('body').on('keyup', 'input', function() {
// do something
});
});
</script>
</head>

A working example including the regex you need can be found at http://jsfiddle.net/5XXJR/

JQuery function to allow only alphabets in textbox not working

Hope this helps:

<!DOCTYPE html>

<html lang="en">
<head>
<script src="jquery-1.12.2.js"></script>
</head>
<body>
<input class="txtOnly" id="txtOnly" name="txtOnly" type="text" />
<script>
$( document ).ready(function() {
$( ".txtOnly" ).keypress(function(e) {
var key = e.keyCode;
if (key >= 48 && key <= 57) {
e.preventDefault();
}
});
});
</script>
</body>
</html>

How to force input to only allow Alpha Letters?

The property event.key gave me an undefined value. Instead, I used event.keyCode:

function alphaOnly(event) {
var key = event.keyCode;
return ((key >= 65 && key <= 90) || key == 8);
};

Note that the value of 8 is for the backspace key.

Is there a way to accept only alphabets as input and not numbers? If numbers are given it should show a validation error as it takes only alphabets

You can do validation using pattern like this.

<form action="/action_page.php">
First name: <input type="text" pattern="[A-Za-z]" name="fname"><br>
Last name: <input type="text" pattern="[A-Za-z]" name="lname"><br>
<input type="submit" value="Submit">
</form>

or validation can be done by Jquery. input fields will not accept any other input except alphabets.

<!DOCTYPE html>

<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
First name <input type="text" pattern="[A-Za-z]" id="fname" name="fname"><br>
Last name <input type="text" pattern="[A-Za-z]" id="lname" name="lname"><br>
<script>
$( document ).ready(function() {
$( "#fname" ).keypress(function(e) {
var key = e.keyCode;
if (key >= 48 && key <= 57) {
e.preventDefault();
}
});
$( "#lname" ).keypress(function(e) {
var key = e.keyCode;
if (key >= 48 && key <= 57) {
e.preventDefault();
}
});
});
</script>
</body>
</html>

Allow text box only for letters using jQuery?

<input name="lorem" onkeyup="this.value=this.value.replace(/[^a-z]/g,'');">

And can be the same to onblur for evil user who like to paste instead of typing ;)

[+] Pretty jQuery code:

<input name="lorem" class="alphaonly">
<script type="text/javascript">
$('.alphaonly').bind('keyup blur',function(){
var node = $(this);
node.val(node.val().replace(/[^a-z]/g,'') ); }
);
</script>

Don't allow typing alphabetic characters in a <input type=number />

You can use jquery.numeric plugin.

See
here similar question.

$(document).ready(function(){
$(".numeric").numeric();
});


Related Topics



Leave a reply



Submit