Re: Simple C# validation.
Post your attempt....[in a minimal yet complete form]
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.
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
}
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.
Re: Simple C# validation.
Doesn't any one use code tags :confused: :confused:
I will look at the posts, once they have been edited....
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
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.
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
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.
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
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");
}
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
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.
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