Jump to content

Visual C# text box validation

- - - - -

  • Please log in to reply
19 replies to this topic

#1
welton122

welton122

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
hi all,

ok i am creating some programs (trying to learn visual C#) but i am abit stuck on how to validate a users input. I have the text box, but i want to be able to check to make sure that they are entering a integer and not characters.

How is it i can do this?

Many Thanks,
J

#2
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
  • Programming Language:Java, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Lua, ActionScript
There's actually no easy way doing this. You have to check each character if it's numeric or not.

    bool isNumeric(String str)

        {

            bool valid = true;

            bool seperator = false;

            foreach (Char c in str.Trim())

            {

                if (!char.IsNumber(c))

                {

                    if (c == '.' && !seperator)

                    {

                        seperator = true;

                    }

                    else

                    {

                        valid = false;

                        break;

                    }

                }

            }

            return valid;

        }


#3
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 242 posts
There is an easy way, use a MaskedTextBox instead. Set the Mask property to whatever you need. For example, if the number must be 5 digits long you'd use "00000". If it could be up to 5 digits in length you would use "99990" (9 means optional digit, 0 means required digit). You can read more about it at the provided link.

You could also attach a handler to the Validating event and validate the entire input. Or attach a handler to the keypress event and validate the individual keypresses.

It all depends on what you need to do and how you want to do it.

#4
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
  • Programming Language:Java, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Lua, ActionScript
That will only work if you only allows a few number of digits, good luck writing in the whole mask for 2 billion characters. :D

#5
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 242 posts

Vswe said:

That will only work if you only allows a few number of digits, good luck writing in the whole mask for 2 billion characters. :D

He said an integer, so not going to be more than a ten digit mask.

#6
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
  • Programming Language:Java, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Lua, ActionScript
It will still be an integer if it has 2 billion chars. It won't be an Int32, but still an integer.

#7
gaylo565

gaylo565

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 268 posts
There are a few ways to do this as you have seen already. My favorite was the already mentioned validator attached to an event handler. You can do this either for each keystroke (wich is cool but inefficient) or on the final submit buttons click handler event (or other control) .
private void Button1_Click(object sender, EventArgs e) 
{ 
    try 
    { 
        int.Parse(TXT_Number.Text); 
    } 
    catch 
    { 
        LBL_NumberError.Text = "You have not entered a number"; 
    } 
} 
The code above validates the number and sends an error message to a label if the format is incorrect. This way isn't any better but you do get an exception back, wich can be displayed to the user so they know they typed something in wrong and must re-enter the info. Plus I think you will find this to be the most commonly used method.

#8
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 242 posts

gaylo565 said:

The code above validates the number and sends an error message to a label if the format is incorrect. This way isn't any better but you do get an exception back, wich can be displayed to the user so they know they typed something in wrong and must re-enter the info. Plus I think you will find this to be the most commonly used method.
It is very common, but it's wasteful of resources. Never throw an exception as a logic test:
private void Button1_Click(object sender, EventArgs e) {
    int r;
    if (Int32.TryParse(TXT_Number.Text, out r)) {
        // value is a number and is stored in r
    } else {
        // value is not a number ... do what you need
    }
}


#9
gaylo565

gaylo565

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 268 posts

Momerath said:

It is very common, but it's wasteful of resources. Never throw an exception as a logic test:
lol...This is not a logic test??? This is to check user input, and an exception is a pretty good way to go, for the reason I already gave.

#10
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 242 posts

gaylo565 said:

lol...This is not a logic test??? This is to check user input, and an exception is a pretty good way to go, for the reason I already gave.

How is it not a logic test? You are 'asking' if the number is an integer, if it isn't, you throw an exception. Bad practice.

#11
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
  • Programming Language:Java, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Lua, ActionScript
I have to agree, it's very bad practice to use exceptions as a logic test.

#12
gaylo565

gaylo565

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 268 posts
Ok...there are certainly more itterations caused by throwing exceptions and thus it is a more resource intensive task. An exception isn't the most efficient way. As for not using exceptions for logic tests what is a divide by zero exception if it isn't an if statement checking if the denominator is zero?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users