469,361 Members | 2,376 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,361 developers. It's quick & easy.

Accept only number in a text box in asp.net with VB

mgpsivan
while entering data to text box it should only accept the numbers and no text should be accepted.. so how should this be done... please do reply..
Feb 28 '07 #1
10 14517
while entering data to text box it should only accept the numbers and no text should be accepted.. so how should this be done... please do reply..

Hello, try this ... at the "Leave" of the control

dim tx_Value as long

if txt_Box.text = space(0) then
exit sub
endif

try
tx_value = txt_Box.text
catch
msgbox("this text box only accept numbers")
end try


Bye
Feb 28 '07 #2
Frinavale
9,735 Expert Mod 8TB
Are you trying to let your user enter whatever they'd like and then checking for invalid data using code on the server ?

Or

Are you trying to prevent the user from even entering in the information in the first place?

If the second case is what you're trying to do, I believe that you will have to use JavaScript to prevent the text from ever being entered. This means you'll have to develop a client side script that simply will not allow input that doesn't match the type you're expecting. Be aware that some users can by-pass this check so it is advisable to also validate the data in your server side logic.

There is no way to really prevent users from entering certain text this using pure .NET; however, there is a nice extension developed by Microsoft for .NET that will filter your textboxes for you. If you're interested, check out the package offered by Microsoft called: ASP.NET AJAX

Cheers!

-Frinny
Feb 28 '07 #3
while entering data to text box it should only accept the numbers and no text should be accepted.. so how should this be done... please do reply..
Hi,

Try paste this code in the KeyPress event of the text box

If Asc(e.KeyChar) >= 48 And Asc(e.KeyChar) <= 57 Then
e.Handled = False
ElseIf Asc(e.KeyChar) = 46 Then
e.Handled = False
Else
e.Handled = True
End If

Thanks and Regards,

Prashant Hirapara
Jul 13 '07 #4
radcaesar
759 Expert 512MB
private void HandleKeyPress( object sender, KeyPressEventArgs e )
{
if( !char.IsDigit( e.KeyChar ) )
{
e.Handled = true;
}
}
Jul 13 '07 #5
Frinavale
9,735 Expert Mod 8TB
Wow this is an old thread!

I have since written an article on this because it's such a commonly asked question.

Check out How to check if a textbox contains a number for more detail on this topic :)

The article gives you a two of ways to check this: using JavaScript and using .NET... it is advisable to use both methods to ensure that the data is valid.

Cheers!

-Frinny
Jul 13 '07 #6
Hi Prashanth,
your posting was very helpful.
i being a fresher in C# looks forward to solutions that are simple.
you are doing a great job.
keep up the good work.....
Thanking you,
Jiju A Nair
Oct 3 '07 #7
Shashi Sadasivan
1,435 Expert 1GB
You could make use of a regular expression validator [1-9]{1}[0-9]*

will reduce postbacks

cheers
Oct 3 '07 #8
ktraju
1
by using regular expression validator control ur problem will be solved.
Jan 29 '08 #9
while entering data to text box it should only accept the numbers and no text should be accepted.. so how should this be done... please do reply..
you should use javascript

var value = document.getElementById("TextBox").value
if(isNaN(value) == true)
{
alert("This is not a number!");
}
Jan 31 '08 #10
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim num As Integer
num = Asc(e.KeyChar)
If num = 8 Or num = 32 Then
Exit Sub
End If

If (Not (num >=48 And num <= 57 )) Then
e.Handled = True
End If
End Sub
Aug 6 '08 #11

Post your reply

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

Similar topics

6 posts views Thread by Woody Splawn | last post: by
2 posts views Thread by StanB | last post: by
10 posts views Thread by sparks | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.