Click to See Complete Forum and Search --> : Simple C# validation.


meripu1
May 3rd, 2007, 12:26 PM
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?

TheCPUWizard
May 3rd, 2007, 12:44 PM
Post your attempt....[in a minimal yet complete form]

meripu1
May 3rd, 2007, 12:49 PM
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.

Traps
May 3rd, 2007, 01:02 PM
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

try
{
int number = input;
// save the input
}
catch
{
// error
}

meripu1
May 3rd, 2007, 01:16 PM
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.

TheCPUWizard
May 3rd, 2007, 01:28 PM
Doesn't any one use code tags :confused: :confused:

I will look at the posts, once they have been edited....

laitinen
May 3rd, 2007, 01:31 PM
You could check the input while the user types in the textbox.


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

Laitinen

Traps
May 3rd, 2007, 01:38 PM
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


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.

laitinen
May 3rd, 2007, 02:23 PM
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

meripu1
May 3rd, 2007, 03:08 PM
You could check the input while the user types in the textbox.


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.

laitinen
May 3rd, 2007, 03:31 PM
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

meripu1
May 3rd, 2007, 03:47 PM
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");
}

laitinen
May 3rd, 2007, 03:58 PM
With that code the message box should always pop up. It has to be changed to this:

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

meripu1
May 3rd, 2007, 04:04 PM
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.

laitinen
May 3rd, 2007, 04:09 PM
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

meripu1
May 3rd, 2007, 04:12 PM
1 - I added the event handler how you told me to do it.
2 - You are allowed to write numbers. This is what im trying to stop. At the minute, if i type a number into the text box, they appear as normal.
3 - The only code in that button is what you have provided:

private void nameTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (Char.IsNumber(e.KeyChar))
{
e.Handled = true;
MessageBox.Show("Error");
}
}

TheCPUWizard
May 3rd, 2007, 04:28 PM
If you already had this function and then tried to add the event handler, you would get a routine named: nameTextBox_KeyPress1.

Check back in the form initialization to see what method is being assoiciated with the KeyPressed Event.....

meripu1
May 3rd, 2007, 04:31 PM
Sorted it!
It was due to another, unrelated compilation error (from another problem that would be way too complicated to explain, unfortunately).
Its working perfectly now - thank you so much.

TheCPUWizard
May 3rd, 2007, 07:23 PM
It was due to another, unrelated compilation error

How/Why did you RUN the program with a Compilation Error??? :confused: :confused: