473,386 Members | 1,830 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,386 software developers and data experts.

Validating a text box

How do I validate a text box field which only requires a number in it?

Nov 1 '07 #1
5 2415
On Thu, 01 Nov 2007 09:09:55 -0700, JJ297 <nc***@yahoo.comwrote:
>How do I validate a text box field which only requires a number in it?
Couple of options
1) Use a regular expression to validate the text \d+
2) Don't use a textbox at all -- use something like a numeric up down

--
http://bytes.thinkersroom.com
Nov 1 '07 #2
Hi there,

Use RangeValidator with Type set to "Integer". You can also specify min and
max values as well by changing MinimumValue & MaximumValue properties
respectively. Note validation succeeds if the input control is empty. Apply
RequiredFieldValidator control to make the input control a mandatory field.

Hope this helps
--
Milosz
"JJ297" wrote:
How do I validate a text box field which only requires a number in it?

Nov 1 '07 #3

Hi,

The other option is to convert all the characters of textbox to ascii
and check whether they are numbers or not.
Here is an example,
string tempString = Textbox1.Text.ToString().Trim();
if ((tempString != "") && (tempString != null))
{
for (int i = 0; i < tempString.ToString().Length; i++)
{
int iAsciiCode = Convert.ToInt32(((int)tempString[i]).ToString());
if (iAsciiCode != 46)
{
if (!((iAsciiCode 47) && (iAsciiCode < 58)))
{
//some message that it should be number
return;
}
}
}
}
Regards,
Mansi Shah.

*** Sent via Developersdex http://www.developersdex.com ***
Nov 2 '07 #4
On Nov 1, 7:15 pm, Milosz Skalecki [MCAD] <mily...@DONTLIKESPAMwp.pl>
wrote:
Hi there,

Use RangeValidator with Type set to "Integer". You can also specify min and
max values as well by changing MinimumValue & MaximumValue properties
respectively. Note validation succeeds if the input control is empty. Apply
RequiredFieldValidator control to make the input control a mandatory field.

Hope this helps
--
Milosz

"JJ297" wrote:
How do I validate a text box field which only requires a number in it?- Hide quoted text -

- Show quoted text -
Thanks Milosz

I got it to work but when I enter in number all of my validation comes
up on the page how can I just get the error to say enter a number to
only come up and not the other messages?

Nov 2 '07 #5
On Nov 2, 11:55 am, JJ297 <nc...@yahoo.comwrote:
On Nov 1, 7:15 pm, Milosz Skalecki [MCAD] <mily...@DONTLIKESPAMwp.pl>
wrote:


Hi there,
Use RangeValidator with Type set to "Integer". You can also specify min and
max values as well by changing MinimumValue & MaximumValue properties
respectively. Note validation succeeds if the input control is empty. Apply
RequiredFieldValidator control to make the input control a mandatory field.
Hope this helps
--
Milosz
"JJ297" wrote:
How do I validate a text box field which only requires a number in it?- Hide quoted text -
- Show quoted text -

Thanks Milosz

I got it to work but when I enter in number all of my validation comes
up on the page how can I just get the error to say enter a number to
only come up and not the other messages?- Hide quoted text -

- Show quoted text -
I got it working by using a regular expression validator instead.

Nov 2 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: Toni Mcintyre | last post by:
i'm having 2 problems with the http://validator.w3.org 1. if i have: <meta http-equiv="Content-Script-Type" content="text/javascript"> then why do i need <script type=text/javascript>...
6
by: Alex Bink | last post by:
Hi, I have a validating event on a textbox in which I want to prevent the user to leave the textbox without entering the right data. Only if he clicks on another specific control he is allowed...
0
by: Matthew | last post by:
All, I have searched google and the newsgroups but can't find anything the same as what I am experiencing (though I may have missed something). I have controls (textboxes) within UserControls...
0
by: Gary Shell | last post by:
I am experiencing some strange behavior between a UserControl's validating event and a treeview control. Initially, I thought it was related to an issue in the Knowledgebase article 810852...
5
by: Ian Jewitt | last post by:
Hi, I am having a problem where the validating event of a text box is not always firing. I am using vb.net 2003. I have an MDI application, one of the child forms contains a tab...
21
by: Darin | last post by:
I have a form w/ a textbox and Cancel button on it. I have a routine to handle textbox.validating, and I have the form setup so the Cancel button is the Cancel button. WHen the user clicks on...
16
by: Al Santino | last post by:
Hi, It appears displaying a messagebox in a validating event will cancel the subsequent event. In the program below, button 2's click event doesn't fire if you open a dialog box in button 1's...
5
by: ameen.abdullah | last post by:
Hi Guys, I have a textbox in windows form that should only accept alphabets, numbers, spaces and underscore. If the textbox contains anyother character it should display a msg at the time of...
1
by: lenin42001 | last post by:
Help i've just done a basic calculator with three textboxes a label & a button, basically when you add a number in textbox1 to the one in textbox2 it appears in textbox3 .........now the question...
1
by: =?Utf-8?B?bGpsZXZlbmQy?= | last post by:
I've noticed that controls do not raise a Validating event if they are contained in a ToolStripDropDown via a ToolStripControlHost item. Please run the following sample and follow the instructions...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.