CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Join Date
    May 2007
    Posts
    25

    Simple C# validation.

    I am having trouble with simple validation.
    All I want to do is validate a text box so that if a number is entered, an error occurs.

    I can check lengths and for nulls but I cant figure this one out.

    I have tried using a char.IsDigit but that doesnt work, and apparently it isnt as simple as string.contains = 1,2,3 etc.

    Im really stuck. Can anybody help me out?

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Simple C# validation.

    Post your attempt....[in a minimal yet complete form]
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    May 2007
    Posts
    25

    Re: Simple C# validation.

    All Im wanting to do is:

    If (input = contains a numeric character)
    {
    Error
    }
    else
    {
    Save the input
    }

    Im sure you can guess that im not very experienced.

  4. #4
    Join Date
    Mar 2007
    Posts
    274

    Re: Simple C# validation.

    I'm sure there's a better way but.... I think this might work, not sure if you'll get a compile error or not. Code assumes input is type string

    Code:
     try 
    {
    int number = input;
    // save the input
    }
    catch
    {
    // error
    }
    Last edited by Traps; May 3rd, 2007 at 01:39 PM.

  5. #5
    Join Date
    May 2007
    Posts
    25

    Re: Simple C# validation.

    What I have so far is:

    public string CheckName(string inName)
    {
    if (inName.Length == 0)
    {
    return "Empty Name";
    }
    return "";
    }

    Im trying to get it to be more like:

    public string CheckName(string inName)
    {
    if (inName.Length == 0) || (inName.Contains == [1,2,3,4,5,6,7,8,9,0])
    {
    return " InputError";
    }
    return "";
    }

    Or I could do it in the actual form so that as soon as you typed in a number, an error message would appear. I know this can be done as I have seen it - but I dont know how to do it.

  6. #6
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Simple C# validation.

    Doesn't any one use code tags

    I will look at the posts, once they have been edited....
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  7. #7
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: Simple C# validation.

    You could check the input while the user types in the textbox.

    Code:
    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if(Char.IsNumber(e.KeyChar))
            e.Handled = true;
    }
    Laitinen

  8. #8
    Join Date
    Mar 2007
    Posts
    274

    Re: Simple C# validation.

    Did you try my code sample? I'm not at visual studio to test it. Looks like the best idea out of the pool of suggestions thus far.

    Actually looking at your request again, you just wanna see if your input string has a numeric character, then

    Code:
     
    bool saveit = true
    foreach (char c in inputsring) 
    {
    switch (c)
    case "1",2,3,4,5,6,7,8,9,0.....
    saveit = false
    break;
    default
     
    }
     
    if (saveit)
    {
    //save it
    {
    again, i'm sure there's an easier way.
    Last edited by Traps; May 3rd, 2007 at 01:55 PM.

  9. #9
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: Simple C# validation.

    Quote Originally Posted by Traps
    again, i'm sure there's an easier way.
    Yes there is, cant see it much easier than handle the input while the user types. If you want to you can show a MessageBox with an apropriate message to the user.

    Laitinen

  10. #10
    Join Date
    May 2007
    Posts
    25

    Re: Simple C# validation.

    Quote Originally Posted by laitinen
    You could check the input while the user types in the textbox.

    Code:
    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if(Char.IsNumber(e.KeyChar))
            e.Handled = true;
    }
    Laitinen
    Thank you, this is exactly what I want to do. However, I tried to use it and the compiler complained that "No overload for 'nameTextBox_KeyPress' matches delegate 'System.EvenetHandler'". What is causing this?
    If you could help me out with that, I would really appreciate as it is exactly what I want to do. Thanks.
    Last edited by meripu1; May 3rd, 2007 at 03:13 PM.

  11. #11
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: Simple C# validation.

    Easiest way if you are using Visual Studio, click the textbox, in the properties window select the "Event" button. Double-click the cell to the right of "KeyPress". This will add an event handler for the KeyPress event for your button.

    Laitinen

  12. #12
    Join Date
    May 2007
    Posts
    25

    Re: Simple C# validation.

    Its compiling properly now but it doesnt work.
    I put a break point in, compiled it and clicked into the text box. Pressing a number didnt bring up the message box.

    private void nameTextBox_KeyPress_1(object sender, KeyPressEventArgs e)
    {
    if (Char.IsNumber(e.KeyChar))
    e.Handled = true;
    MessageBox.Show("Error");
    }
    Last edited by meripu1; May 3rd, 2007 at 03:50 PM.

  13. #13
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: Simple C# validation.

    With that code the message box should always pop up. It has to be changed to this:
    Code:
    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (Char.IsNumber(e.KeyChar))
                {
                    e.Handled = true;
                    MessageBox.Show("Error");
                }
            }
    However, this does not answer why your message box is not showing. You have made some mistakes somewhere along the way. Are you allowed to type numbers in the textbox?

    Laitinen

  14. #14
    Join Date
    May 2007
    Posts
    25

    Re: Simple C# validation.

    There is absoloutely nothing else on the text box. Its a brand new text box.
    I changed it to what you put and it still isnt entering the code.

  15. #15
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: Simple C# validation.

    1) How did you add the event handler? By hand?
    2) Are you allowed to write numbers in the textbox?
    3) Post a small but still working example of your code.

    Laitinen

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured