Introduction:
Here I will show how to use JQuery to allow only alphanumeric
characters in textbox or make textbox to allow only alphabets & numbers using JQuery.
Description:
In previous posts I explained jQuery allow alphabetic characters in textbox, jQuery Allow only numbers in textbox, jQuery disable cut copy and paste options in textbox, jQuery Create Rounded Corners Textbox, jQuery slideUp, slideDown and slideToggle Example and many articles relating to JQuery, JavaScript, Asp.net. Now I will explain how to allow only alphanumeric characters in textbox using JQuery.
In previous posts I explained jQuery allow alphabetic characters in textbox, jQuery Allow only numbers in textbox, jQuery disable cut copy and paste options in textbox, jQuery Create Rounded Corners Textbox, jQuery slideUp, slideDown and slideToggle Example and many articles relating to JQuery, JavaScript, Asp.net. Now I will explain how to allow only alphanumeric characters in textbox using JQuery.
To allow only alphabets & numbers in textbox using JQuery
we need to write the following code
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jQuery
Allow only alphanumeric characters in textbox</title>
<script type="text/javascript"
src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
$(function ()
{
$('#txtNumeric').keydown(function (e) {
if (e.shiftKey || e.ctrlKey || e.altKey) {
e.preventDefault();
} else {
var key = e.keyCode;
if (!((key == 8) || (key == 32) || (key == 46) || (key
>= 35 && key <= 40) || (key >= 65 && key <= 90)
|| (key >= 48 && key <= 57) || (key >= 96 && key
<= 105))) {
e.preventDefault();
}
}
});
});
</script>
</head>
<body>
<div>
<b>Enter
Text:</b><input type="text"
id="txtNumeric"
/>
</div>
</body>
</html>
|
Live Demo
To check live demo try to enter text in below textbox
|
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
10 comments :
it so good ,,, but we need alphabets only
plzzz
remove this (key >= 48 && key <= 57) from the condition it will allow only alphabets.Please try it once .
Good, can you write more about textbox info.
Man.. You are awesome. Thanks for helping me out
ye to sab leta he
if they want to enter capital letter using SHIFT A ,so how it will work.,, Suresh
thank u..
This code is working fine but i want to paste numeric value onlym how it is possible
how to apply this script for multiple textbox
Note: Only a member of this blog may post a comment.