Welcome to Dream.In.Code
Getting Help is Easy!

Join 105,765 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,604 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Numbers only

 
Reply to this topicStart new topic

Numbers only, Numbers only can type in a textbox using asp.net and javascript

ryan john majarais
post 1 May, 2008 - 01:57 AM
Post #1


New D.I.C Head

*
Joined: 3 Apr, 2008
Posts: 5


My Contributions


smile.gif im using asp.net code and javascript
theres an error telling me that 'Onkeypress' is not a valid attribute of element 'TextBox

because i need a textbox that you can only type numeric only numbers only...

what is wrong to my code?
pls give me the correct one.

jscript

<script language=javascript>
function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;

return true;
}

</script>


HTML
<body>
<form id="form1" runat="server">
<div>

<asp:TextBox ID="TextBox1"Onkeypress= "return isNumberKey(event)" runat="server" BorderStyle="Solid" Style="z-index: 100;
left: 184px; position: absolute; top: 99px"></asp:TextBox>


This post has been edited by PsychoCoder: 1 May, 2008 - 04:45 AM
User is offlineProfile CardPM

Go to the top of the page


PsychoCoder
post 1 May, 2008 - 04:48 AM
Post #2


DIC.Rules == true;

Group Icon
Joined: 26 Jul, 2007
Posts: 7,030



Thanked 47 times

Dream Kudos: 7600

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, GDI

My Contributions


Thats because there isnt an OnKeyPress event on the ASP.Net TextBox. YOu can add one in your code behind file like this (In your form's load event


VB.Net
vb

TextBox1.Attributes.Add("onkeypress", "return isNumberKey(event);")


C#
csharp

TextBox1.Attributes.Add("onkeypress", "return isNumberKey(event);");



Then remove the OnKeyPress you've added to the HTML portion of your TextBox

HTML
<asp:TextBox ID="TextBox1" runat="server" BorderStyle="Solid" Style="z-index: 100;
left: 184px; position: absolute; top: 99px"></asp:TextBox>



Hope that helps smile.gif
User is online!Profile CardPM

Go to the top of the page

thor78
post 6 May, 2008 - 08:53 PM
Post #3


D.I.C Head

Group Icon
Joined: 6 May, 2008
Posts: 104



Dream Kudos: 50
My Contributions


You can also use a RegularExpressionValidator. Use the regular expression \d* in the ValidationExpression property of the RegularExpressionValidator to limit the text to numeric characters only.
In the textbox_textchanged event you can also insert the following:

CODE

protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        RegularExpressionValidator1.Validate();
    }


But this only checks after the input (if you move the cursor away from the textbox), not immediately after the key press.
User is offlineProfile CardPM

Go to the top of the page

JasonMcAuley
post 7 May, 2008 - 05:17 AM
Post #4


D.I.C Head

**
Joined: 10 Apr, 2008
Posts: 134


My Contributions


Take a look at this ajax control. It can do exactly what you want.

http://asp.net/AJAX/AjaxControlToolkit/Sam...redTextBox.aspx
User is offlineProfile CardPM

Go to the top of the page

thor78
post 7 May, 2008 - 05:43 PM
Post #5


D.I.C Head

Group Icon
Joined: 6 May, 2008
Posts: 104



Dream Kudos: 50
My Contributions


Yeah, but then he'd have to include the Ajax dll, and script managers and the control itself. smile.gif
User is offlineProfile CardPM

Go to the top of the page

JasonMcAuley
post 8 May, 2008 - 08:33 AM
Post #6


D.I.C Head

**
Joined: 10 Apr, 2008
Posts: 134


My Contributions


QUOTE(thor78 @ 7 May, 2008 - 05:43 PM) *

Yeah, but then he'd have to include the Ajax dll, and script managers and the control itself. smile.gif


Relatively easy; but I suppose its a matter of preference smile.gif
User is offlineProfile CardPM

Go to the top of the page

mnn888
post 15 May, 2008 - 04:21 PM
Post #7


New D.I.C Head

*
Joined: 10 Dec, 2007
Posts: 18


My Contributions


or you can change your asp:Textbox to a htmlcontrol textbox:

<input type="text" ID="TextBox1" Onkeypress= "return isNumberKey(event)" runat="server" />

now you access the texbox text with TextBox1.Value instead of TextBox1.Text


QUOTE(ryan john majarais @ 1 May, 2008 - 01:57 AM) *

smile.gif im using asp.net code and javascript
theres an error telling me that 'Onkeypress' is not a valid attribute of element 'TextBox

because i need a textbox that you can only type numeric only numbers only...

what is wrong to my code?
pls give me the correct one.

jscript

<script language=javascript>
function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;

return true;
}

</script>


HTML
<body>
<form id="form1" runat="server">
<div>

<asp:TextBox ID="TextBox1"Onkeypress= "return isNumberKey(event)" runat="server" BorderStyle="Solid" Style="z-index: 100;
left: 184px; position: absolute; top: 99px"></asp:TextBox>


User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 8/21/08 02:32PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month