Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

how to restrict user to enter only numbers in textbox using javascript and how to restrict user to enter only 10 digits(mobile number) in textbox using javascript or how to restrict user to enter only alphanumeric in textbox using javascript

Dec 30, 2010
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.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

31 comments :

Raghavendra said...

thank you very much'i am searching this code from a long time...,

purushothama said...

I really Appreciate U to Post this Webpage.
why because it will be very usefull to all who ever do the Client side validations.

Anonymous said...

backspace or delete button unte baguntundhi kada. number delete chesi correct one enter chestharu

jo said...

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..

Suresh Dasari said...

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.

srinivas said...

In this validations not working back spaces

Siva said...

hi , good script . But it cannot be delete , so what is the step to delete..

CRYSTALOOK said...

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

Surendra Reddy said...

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

Anonymous said...

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

Suresh Dasari said...

hi all,
I updated above code now textbox allow only numbers and it will allow backspace and delete keys also....

Kuppappa Harijan said...

Thanks a lot man. I was searching validation code like this from long time.

senthilarasan said...

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

sindhu said...

hi suresh this code is working perfectly..Thank u very much...

mohan kumar said...

i want the restriction in vbscript for the text box values

mohan kumar said...

Different types of restriction in vbscript is needed to test the validation of the page

rohit said...

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

Anonymous said...

thank u great post

Piyush said...

it's working.......
Thanks dude.

Anonymous said...

can i know what's going on at "key.which"

shweta said...

Thanks for your post

Mohammed Ahammed said...

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"

Divya Jammula said...

HI suresh i just want to restrict the number to 9 digits only what should i do

Anonymous said...

on my page validation control not working what should i do
plz help its problem

Anonymous said...

Is this code working with EditorFor in MVC 4...

Anonymous said...

hi suresh, i am not getting alert on entering dot (.) in the textbox

Anonymous said...

how to restirct user to enter 3 digit numbers from 1 to 250 in textbox using javascript/html5

Anonymous said...

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??

Chennarao Gadicherla said...

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


Ganesh Ch said...

It is allowing dot(.) symbol

Mahendra Gohil said...

Dear I have VS2010. in VS2010 textbox does not have onkeypress event. what should I do??
www.gtuguide.com

Give your Valuable Comments

Other Related Posts

© 2010-2012 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.