Introduction
Here I will explain how to restrict user to allow only numbers in textbox and how to restrict user to enter only 10 digits mobile number using JavaScript and I will explain how to allow only numbers and alphabets in textbox using JavaScript.
Description
I have a one textbox now I need to allow user to enter only numbers not more than that if user try to enter characters or alphabets I don’t want to allow those characters in textbox for that I have written one JavaScript function to allow only numbers and allow only 10 numbers.
To implement this you need to write below code in your aspx page
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Allow Only Numbers and alpahabates Page</title>
<script type="text/javascript">
//Function to allow only numbers to textbox
function validate(key)
{
//getting key code of pressed key
var keycode = (key.which) ? key.which : key.keyCode;
var phn = document.getElementById('txtPhn');
//comparing pressed keycodes
if (!(keycode==8 || keycode==46)&&(keycode < 48 || keycode > 57))
{
return false;
}
else
{
//Condition to check textbox contains ten numbers or not
if (phn.value.length <10)
{
return true;
}
else
{
return false;
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtPhn" runat="server" onkeypress="return validate(event)"></asp:TextBox>
</div>
</form>
</body>
</html>
|
Demo
Please enter text in below textbox
|
After that here we will see how to restrict user to
enter only alphanumeric in textbox
To implement this you need to write below code in your aspx page
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Allow Only Numbers and alpahabates Page</title>
<script type="text/javascript">
//Function to allow only alpha
numeric to textbox
function validatealphanumeric(key) {
var keycode = (key.which) ? key.which : key.keyCode
var phn = document.getElementById('txtChar');
//comparing pressed keycodes
if (!(keycode==8 || keycode==46)&&(keycode < 48 || keycode > 57) && (keycode
< 97 || keycode > 122)) {
return false;
}
else {
return true;
}
}
</script>
</head>
<body>
<form id="form1"
runat="server">
<div>
<asp:TextBox ID="txtname"
runat="server"
onkeypress="return
validatealphanumeric(event)"></asp:TextBox>
</div>
</form>
</body>
</html>
|
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. |
|||
|
|||
39 comments :
thank you very much'i am searching this code from a long time...,
I really Appreciate U to Post this Webpage.
why because it will be very usefull to all who ever do the Client side validations.
backspace or delete button unte baguntundhi kada. number delete chesi correct one enter chestharu
from d textbox backspace is not working,delete is not working... only i have to select d no which i have written n cut it..
hi jo
if you do this example in your application it will work perfectly. To arrange live demo in website i did this sample using html controls because of that small problem is there with backspace or delete buttons.
In this validations not working back spaces
hi , good script . But it cannot be delete , so what is the step to delete..
Hi this is good but this text box is unable to edit or delete the text we have entered if we does not found an edit option, it is of useless.
please post the code for editing also...
Thanks
Ganesh
Hi Suresh,
This is surendra, i want to print the grid view data in table-er format using list<> data source collection.
Can you help this.. Pls
mail to:v.suri87@gmail.com
Its a good example for restricting in the asp.net server textbox using javascript code. But I have a question: how to get data from database using onkeyup event of the textbox
hi all,
I updated above code now textbox allow only numbers and it will allow backspace and delete keys also....
Thanks a lot man. I was searching validation code like this from long time.
Dear Suresh it is working fine in normal webpage which contain the javascript . if i keep the javascript as seperate file . how can i implement the code. because my page is inherited from master page so i can't use javascript with in the page
hi suresh this code is working perfectly..Thank u very much...
i want the restriction in vbscript for the text box values
Different types of restriction in vbscript is needed to test the validation of the page
hey Suresh... The concept of our site is out standing ..it gives us a perfect idea ,how to impliment code ..really a great site.. for ASP.NET developers...
is their any article related to SQL INJECTION
if yes plz post or send me the link...
..thank you
thank u great post
it's working.......
Thanks dude.
can i know what's going on at "key.which"
Thanks for your post
hi i used ths following code but i am getting jscript error"Unable to get value of the property 'value': object is null or undefined"
HI suresh i just want to restrict the number to 9 digits only what should i do
on my page validation control not working what should i do
plz help its problem
Is this code working with EditorFor in MVC 4...
hi suresh, i am not getting alert on entering dot (.) in the textbox
how to restirct user to enter 3 digit numbers from 1 to 250 in textbox using javascript/html5
hi
suresh how to restirct user to enter 3 digit numbers from 1 to 250 in textbox using javascript/html5 can u help me please??
Hi Suresh Sir .....
Your Site is very helpful for developers . but here where is updated new code . when we enter numbers it accept 10 digits,ok it's fine and good ,but when enterd all 10 numbers in textbox it can't perform back button and not editing mode .how i solve this problem
It is allowing dot(.) symbol
Dear I have VS2010. in VS2010 textbox does not have onkeypress event. what should I do??
www.gtuguide.com
hi sir i want enter name in textbox allow alphabets and space and dot are taken and phone also enter only numbers and hiffen (-) also taken how using javascript with asp.net
After entering all 10 values it should allow backspace to re-enter number
hii sir.... i want my textbox should accept alphabets(caps n small letters), numbers ,white spaces and special characters like (- @ , . ) plz help me... i need ur help its urgent... hope u will help me soon.. thank u in advance :)
hii sir.... i want my textbox should accept alphabets(caps n small letters), numbers ,white spaces and special characters like (- @ , . ) plz help me... i need ur help its urgent... hope u will help me soon.. thank u in advance :)
hiii sir can u mail me the code
Hi sir I made some changes in that and use for html and php form validation that work or not
hey suresh this is gogilan a/l sarepander can u give me code can accept alphabetical....satpeded lu
This code is working fine but i want to paste numeric value onlym how it is possible
Note: Only a member of this blog may post a comment.