473,320 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,320 developers and data experts.

How to validate for numbers only in textbox using Javascript

In ASP.NET you can use textboxes that can enter only numbers. For this, first you have to create a javascript file.

For creating a javascript file,
first right click on the Project name in Solution Explorer
Then,
select Add New Item -> JScript File-> Ok

If you want, you can give a name to JSCript file. Here I use the name "number.js". After that write the following code in that file.
Expand|Select|Wrap|Line Numbers
  1. function numberonly(e)
  2. {
  3.     var KeyID = (window.event) ? event.keyCode : e.which; 
  4.     if(KeyID>=48 && KeyID<=57)
  5.         return true;
  6.     else
  7.         alert("Please Enter Number Only");
  8.         return false;
  9.  }
  10.  

Save this file. Take the source part of the webform. Add the following code in <head> of html.
Expand|Select|Wrap|Line Numbers
  1. <head>
  2. <script type="text/javascript" src="number.js">
  3. </script>
  4. </head>

Take the source aspx.cs file. Inside the Page_Load function
write the below code.
Expand|Select|Wrap|Line Numbers
  1. TextBox1.Attributes.Add("onkeypress", "return numberonly(event);");

Where TextBox1 is the name of the TextBox where we want to enter the data.
Oct 14 '10 #1
0 7104

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Fernando Lopes | last post by:
Hi. I need to change de css of an asp.net textbox control. So, on mouseover event, the class will be one, and on mouseout event it will be another. Something like: ...
1
by: c.verma | last post by:
I am not able to hide a href element using javascript. Here is my code written on aspx page. On the click of "OK" button, I want to hide href element. But I am getting message: Object required....
2
by: rockdale | last post by:
Hi, All: I have a datagrid that embedded 3 textbox using TemplateColumn. When the user key in value in the first textbox, I need to check to see if this value is between the the values in other...
4
by: mantrid | last post by:
Can anyone see why the checkbox in the code below doesnt get ticked when the textbox is changed ***This javascript function is called in the textbox's onchange event to check it when it is...
1
by: satees | last post by:
Hi, I have a textbox control in Footer Template within datagrid. I need to validate the textbox using client side javascript. When trying it, i got an error that the control not exist. I think the...
6
by: stormcandi | last post by:
Hello, I know this question has been asked everywhere but I cannot find a solution that will help me. I hope someone here has some ideas. I have 9 TextBoxes in a Repeater Control. A user can...
3
by: AbhiLookingForCode | last post by:
i want to check a text field whether the inputs cointain character or number plz help
6
by: venkateshcnpl | last post by:
Hi,I am New To This . I have to check the duplications of values entered in textbox which is placed inside the datagrid using javascript .And the Number of textbox is not constant it is...
6
by: john | last post by:
I have the following textbox setup with Text & ToolTip Bindings as follows; I'm using Visual Studio 2008 VB: <asp:TextBox ID="txtDay1" runat="server" Text='<%# Eval("Day1") %>'...
5
by: Abhishek | last post by:
Hi this is my another validator in javascript to validate the Phone Number :-) <script language='javascript'> function funcCheckPhoneNumber(ctrtxtMobile,e){ if(window.event){ var strkeyIE =...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.