10 Replies - 772 Views - Last Post: 27 September 2013 - 11:27 AM Rate Topic: -----

#1 ngroebner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 18-March 13

textbox.text validation

Posted 26 September 2013 - 09:11 AM

I have a textbox that accepts an applicationID for look up. The application id is of type decimal.

I would like to verify that the user has entered soemthing, which I believe i have working...but in addition to checking that there is an input at all, I would like to validate that it is actually a decimal.

currently I can enter any string of characters and it still goes through...which will throw errors later when attempting to use the data.

any help?

decimal AppID = 0;

                if (string.IsNullOrWhiteSpace(txtAppID.Text))
                {
                    lblMessage.Text = "Please Enter an ApplicationID.";
                    return;
                }
                else
                {
                    try
                    {
                        AppID = Convert.ToDecimal(txtAppID.Text);
                    }
                    catch
                    {
                        lblMessage.Text = "ApplicationID must be a valid decimal, please re-enter.";
                    }
                }




Is This A Good Question/Topic? 0
  • +

Replies To: textbox.text validation

#2 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3835
  • View blog
  • Posts: 13,583
  • Joined: 12-December 12

Re: textbox.text validation

Posted 26 September 2013 - 09:24 AM

Your code should throw an error when the value in the TextBox is not a valid Decimal. So please explain what it is that you are looking to achieve? What is the code not doing that it should be doing?

An alternative would be to use a NumericUpDown control, which only accepts a number.

This post has been edited by andrewsw: 26 September 2013 - 09:26 AM

Was This Post Helpful? 0
  • +
  • -

#3 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 272
  • View blog
  • Posts: 552
  • Joined: 07-July 10

Re: textbox.text validation

Posted 26 September 2013 - 09:26 AM

Try using a MaskedTextBox instead.
Was This Post Helpful? 2
  • +
  • -

#4 ngroebner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 18-March 13

Re: textbox.text validation

Posted 26 September 2013 - 09:30 AM

It is not throwing an exception/error if I enter something. If I leave the textbox empty I recieve the expected message of "please enter an applicationID."

But if I enter anything at all for example "al;skdfj" which is a string of characters, not a decimal, it simply moves on to the next step, which throws an error after that point because it is going to try and pass the decimal to a sql stored procedure parameter...

I think it may have something to do with the variable initialization???

I begin the statment by declaring and initializing the variable AppID = 0;

So if I enter anything into the textbox, its seeming to keep that value at 0 instead of requesting the user to input a valid decimal type.
Was This Post Helpful? 0
  • +
  • -

#5 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 272
  • View blog
  • Posts: 552
  • Joined: 07-July 10

Re: textbox.text validation

Posted 26 September 2013 - 09:36 AM

This is your current code displayed in psuedo code, so you see the problem?

if string is empty
show error
stop processing (return)
else <!-- note that this else is not needed since you returned out of the function already
try convert to decimal
if fail
show error
process decimal
Was This Post Helpful? 0
  • +
  • -

#6 ngroebner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 18-March 13

Re: textbox.text validation

Posted 26 September 2013 - 09:54 AM

I removed the "else" statement, and moved directly to a try/catch for the convert.toDecimal(txtBox.text)....

This is seeming to work at this point...if there is still room for bugs, let me know.

decimal AppID = 0;

                if (string.IsNullOrWhiteSpace(txtAppID.Text))
                {
                    lblMessage.Text = "Please Enter an ApplicationID.";
                    return;
                }
                try
                {
                    AppID = Convert.ToDecimal(txtAppID.Text);
                }
                catch
                {
                    lblMessage.Text = "Please enter a valid decimal type";
                    return;
                }



Was This Post Helpful? 0
  • +
  • -

#7 souptoy  Icon User is offline

  • D.I.C Head


Reputation: 54
  • View blog
  • Posts: 244
  • Joined: 17-January 08

Re: textbox.text validation

Posted 26 September 2013 - 10:32 AM

You could also do the following:

decimal AppID = 0;
decimal.TryParse(txtAppId.Text, out AppID);



TryParse will attempt to convert your string into the decimal base type and set it to the out variable. If it cannot, the out variable will be 0. This, in turn does not throw an exception.

Optionally, you can do the following:

decimal AppID = 0;
bool blnIsConverted = false;
blnIsConverted = decimal.TryParse(txtAppId.Text, out AppID);

if (!blnIsConverted){
    //handle missing value
}



As far as I know, Convert.ToDecimal really does Decimal.Parse so if you have an issue converting the string, an Exception will be thrown.
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3663
  • View blog
  • Posts: 11,482
  • Joined: 05-May 12

Re: textbox.text validation

Posted 26 September 2013 - 05:08 PM

View PostRobin19, on 26 September 2013 - 12:26 PM, said:

Try using a MaskedTextBox instead.


Actually, please do not.

Have you actually tried using the MaskedTextBox for any serious data entry? Its UI sucks rotten eggs through a dirty garden hose. Yes, on paper the idea of the masked text box looks sexy, and it looks like a modern take on the old FoxPro and dBase pattern entry fields. Unfortunately, the person who implemented the masked textbox didn't emulate the behavior of the old FoxPro/dBase fields. Backspacing and the delete key doesn't do what you think it does. Overwrite vs. insert mode doesn't work correctly. Tabbing in and tabbing out, as well as other focus changes don't act like other Windows controls. Additionally the validation events fired seem no return incorrect data.

You are much better off having a plain old textbox, limiting input to digits, the decimal point, and '-', and using TryParse() -- that is if you care at all about the user's experience. If you don't care just drag drop that MaskedTextBox unto your form and call it a day.

Or if you really want to contribute to the community, implement a decent MaskedTextBox and give it to the public domain.
Was This Post Helpful? 1
  • +
  • -

#9 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 272
  • View blog
  • Posts: 552
  • Joined: 07-July 10

Re: textbox.text validation

Posted 27 September 2013 - 09:12 AM

View PostSkydiver, on 26 September 2013 - 07:08 PM, said:

Actually, please do not.

Have you actually tried using the MaskedTextBox for any serious data entry?

I have not used it much or in a production product. I did not realize it was that bad. I will try to erase its existence from my memory. Thanks for correcting me on that.
Was This Post Helpful? 0
  • +
  • -

#10 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3835
  • View blog
  • Posts: 13,583
  • Joined: 12-December 12

Re: textbox.text validation

Posted 27 September 2013 - 09:22 AM

I still have a preference for a NumericUpDown Control. Its Value is a Decimal and it won't accept any text-entry.

If you wish it start blank then you can use:

NumericUpDown1.Text = ""

in the Form's Load event (even though Text doesn't appear in the intellisense). You can also check this .Text value using IsNullOrWhiteSpace(), to confirm whether anything was entered at all.

But.. there's nothing wrong with a TextBox ;)

Added: You wouldn't need to use TryParse either because, if it's not empty or whitespace, then it is a Decimal.

This post has been edited by andrewsw: 27 September 2013 - 10:06 AM

Was This Post Helpful? 1
  • +
  • -

#11 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,500
  • Joined: 29-May 08

Re: textbox.text validation

Posted 27 September 2013 - 11:27 AM

You could always implement your own control (inherited from TextBox)

Tutorial
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1