473,246 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 software developers and data experts.

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

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

Similar topics

5
by: F. Barth | last post by:
Hello, I've posted this problem to one other newsgroups, and gotten some diagnostic help, but I still need a solution. The full text of the message box is: "The field is too small to accept the...
7
by: astro | last post by:
Anyone have suggestions on where to troubleshoot this error? Background: -Access 2k v. 9.0.6926 sp3 - front and backend on production server (wiindows 2k) -accessed via Citrix -front-end is...
6
by: Woody Splawn | last post by:
We have access to Infragistics etc., but we would like to make use of a regular VS textbox so that it accepts only numbers. We have discovered that if you bind an integer field to a textbox, for...
4
by: Joe Schmoe | last post by:
All I want to to be able to take a two-column DataReader (One column with the Item ID number, the other with Item Description text) and load it into a Windows Forms ComboBox (Set to DropDownList...
8
by: lisaj | last post by:
I'm having huge difficulties producing a script for this: Write a javascript programme that will prompt for, and accept from the user, an input string which contains at least 8 characters. It...
0
by: narayan2586 | last post by:
Hi all, When ever i am trying to connect my application it's giving below error message: RemoteException occurred in server thread; nested exception is: java.rmi.RemoteException: ; nested...
2
by: StanB | last post by:
I came across this weird problem: 1. Session state stops working after the app is deployed to another server because IE does not accept cookies. 2. It works if cookieless="true" in the...
10
by: sparks | last post by:
We are entering mm/dd/yyyy but some of the date fields only contain years. Can a date be made to only take a year entry if that is all that they have? can it be made to take only 2008 or maybe...
7
geolemon
by: geolemon | last post by:
I'm getting an error, I'm not seeing the cause: Debug highlights this segment of code (I just snipped a portion here): strProc = "Insert into Parts (MfgPN, Manufacturer, Description)" & _ ...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...

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.