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

validate that string input is a negative number

Hello,

I have a data entry windows form. One of the text boxes allows the user to
enter a string. I need this text box to only allow users to type in a
negative integer value (e.g. -1, -2, -3).

Current approach:
I want to validate the user's entry during the TextChanged() event. During
the validation, I need a function that will parse the string and see if it is
a negative integer. Does anyone have suggestions on the best way to do this?
or alternative approaches from what I have entered?
Mar 27 '07 #1
6 8331
"di****@newsgroup.nospam" <di****@newsgroup.nospam.donotspamwrote in
message news:40**********************************@microsof t.com...
I have a data entry windows form. One of the text boxes allows the user
to
enter a string. I need this text box to only allow users to type in a
negative integer value (e.g. -1, -2, -3).

Current approach:
I want to validate the user's entry during the TextChanged() event.
During
the validation, I need a function that will parse the string and see if it
is
a negative integer. Does anyone have suggestions on the best way to do
this?
or alternative approaches from what I have entered?
Two different suggestions:

- Use int.TryParse to see if it is an integer, and then compare with zero
to see if it's negative.
- Use a regular expression such as "-\d+".

I have not run a benchmark, but I expect the TryParse approach to perform
faster.

Mar 28 '07 #2
Alberto Poblacion wrote:
"di****@newsgroup.nospam" <di****@newsgroup.nospam.donotspamwrote in
message news:40**********************************@microsof t.com...
>I have a data entry windows form. One of the text boxes allows the
user to
enter a string. I need this text box to only allow users to type in a
negative integer value (e.g. -1, -2, -3).

Current approach:
I want to validate the user's entry during the TextChanged() event.
During
the validation, I need a function that will parse the string and see
if it is
a negative integer. Does anyone have suggestions on the best way to
do this?
or alternative approaches from what I have entered?

Two different suggestions:

- Use int.TryParse to see if it is an integer, and then compare with
zero to see if it's negative.
- Use a regular expression such as "-\d+".

I have not run a benchmark, but I expect the TryParse approach to
perform faster.
Also, that regular expression would pass "-0", which isn't negative.

--
Larry Lard
la*******@googlemail.com
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version
Mar 28 '07 #3
"Larry Lard" <la*******@googlemail.comwrote in message
news:56*************@mid.individual.net...
> - Use a regular expression such as "-\d+".
Also, that regular expression would pass "-0", which isn't negative.
Okay, let's refine it a little:

@"^-[1-9]\d*$"
Mar 28 '07 #4


"Alberto Poblacion" <ea******************************@poblacion.orgwro te
in message news:uA**************@TK2MSFTNGP02.phx.gbl...
"Larry Lard" <la*******@googlemail.comwrote in message
news:56*************@mid.individual.net...
>> - Use a regular expression such as "-\d+".
Also, that regular expression would pass "-0", which isn't negative.

Okay, let's refine it a little:

@"^-[1-9]\d*$"

lol sorry, have too:

-01 wouldn't pass the above expression pattern...

@"^-\d*[1-9]\d*$" should...haven't tested though...

Mythran

Mar 28 '07 #5
On Mar 28, 7:47 am, "Mythran" <kip_pot...@hotmail.comwrote:
"Alberto Poblacion" <earthling-quitaestoparacontes...@poblacion.orgwrote
in messagenews:uA**************@TK2MSFTNGP02.phx.gbl. ..
"Larry Lard" <larryl...@googlemail.comwrote in message
news:56*************@mid.individual.net...
> - Use a regular expression such as "-\d+".
Also, that regular expression would pass "-0", which isn't negative.
Okay, let's refine it a little:
@"^-[1-9]\d*$"

lol sorry, have too:

-01 wouldn't pass the above expression pattern...

@"^-\d*[1-9]\d*$" should...haven't tested though...
Or just a nice simple
int val;
return Int32.TryParse(s, out val) && val < 0

Michael
Mar 28 '07 #6
mp*******@gmail.com <mp*******@gmail.comwrote:

<snip>
Or just a nice simple
int val;
return Int32.TryParse(s, out val) && val < 0
Exactly. This thread is a good example of why it's wise to avoid using
regular expressions where they're not truly advantageous :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 28 '07 #7

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

Similar topics

4
by: Jeff | last post by:
Hello there, It's possible to check if it's a valid number in <INPUT... I try typeof and parseFloat and it's not working. It's seem impossible to test (with a if) the value "NaN"!! Regards
10
by: KathyB | last post by:
Hi, I just can't get this quite right. I use the following function to validate a user entry. I need to allow negative numbers including those with decimals (e.g., -.5). The following allows the...
3
by: stanlo | last post by:
hi to everyone, this is still a follow up of my project ,mathematical expression.this project is meant to evaluate mathemtical expressions with oparators,+,-,*,/.more than two operands can be done,...
17
by: jake1138 | last post by:
Here is a function I have to get a number at the end of a string. I'm posting this in case it proves helpful to someone. Comments are welcome. int getnum(char *str) { char buffer; char *buf...
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
5
by: timothy.pollard | last post by:
Hi I'm having a bit of bother trying to make a questionnaire do what I want it to. I have put it up on www.web-iq.co.uk/test.htm. Basically the user of the final form (when I've tarted it up)...
13
by: Freaker85 | last post by:
Hello, I am new at programming in C and I am searching a manner to parse a string into an integer. I know how to do it in Java, but that doesn't work in C ;o) I searched the internet but I...
23
by: codefire | last post by:
Hi, I am trying to get a regexp to validate email addresses but can't get it quite right. The problem is I can't quite find the regexp to deal with ignoring the case james..kirk@fred.com, which...
5
by: phpCodeHead | last post by:
I am needing to determine how to go about validating that a field in my form contains only a positive integer. I know that this is fairly simple if the form contains only one element to be...
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
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...
1
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: 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: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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.