I want to validate the input on a textbox on a Windows Forms application i.e. show a message box with an error if the number entered is lower than 1 or greater than 24 or if any other character besides an integer is entered. How would I go about doing this?
5 Answers
I would consider something like:
//make sure that we have a valid number in the text box with no other characters //loop through the string, examining each character for (int i = 0; i < txtHour.Text.Length; i++) { //if this character isn't a number, then don't close the form or continue if (!char.IsNumber(txtHour.Text[i])) { MessageBox.Show("Value for 'txtHour' must be a number from 1 to 24"); return; } } //now that we know we have a valid number, convert the string to int and make sure it's not less than 1 or greater than 24 int testInt = Convert.ToInt32(txtHour.Text); if (testInt < 1 || testInt > 24) { MessageBox.Show("Value for 'txtHour' must be a number from 1 to 24"); return; } For the method example that you asked for in your comment you could do something more like:
////////////////////////////////////////////////////////////////////// //in your main code: if (!isValidHour(textBox1.Text)) MessageBox.Show("Value for field must be a number from 1 to 24"); if (!isValidHour(textBox2.Text)) MessageBox.Show("Value for field must be a number from 1 to 24"); ////////////////////////////////////////////////////////////////////// ///method to validate if text field is an INT from 1 to 24 bool isValidHour (string stringToValidate) { //make sure that we have a valid number in the text box with no other characters //loop through the string, examining each character for (int i = 0; i < stringToValidate.Length; i++) { //if this character isn't a number, then don't close the form or continue if (!char.IsNumber(stringToValidate[i])) { //MessageBox.Show("Value for 'txtHour' must be a number from 1 to 24"); return false; } } //now that we know we have a valid number, convert the string to int and make sure it's not less than 1 or greater than 24 int testInt = Convert.ToInt32(stringToValidate); if (testInt < 1 || testInt > 24) { //MessageBox.Show("Value for 'txtHour' must be a number from 1 to 24"); return false; } return true; } 4 Comments
try{ if((int)item.value >= 1 && (int)item.value <=25){ //match. }else{ //error. } }catch (Exception e){ //type error } //or--- var itemValue = default(int); if(int.TryParse(item.value, out itemValue)){ if(itemValue >= 1 && itemValue <= 25){ //match. }else{ //error. } }else{ //item.value is not numeric. } 5 Comments
int.Parse here or even better int.TryParse ? What is the meaning of catch block here?TryParse has a second parameter, marked as out. You don't need to parse item.value several times. The error would also be shown, if the value is not a number.item.value is a string and won't even cast.You can add a private method and call it wherever necessary by passing textbox control which needs to be validated.
private void ValidateText(TextBox textbox) { int value; bool isConverted = Int32.TryParse(textbox.Text.Trim(), out value); if (!isConverted) { MessageBox.Show("Only numbers allowed"); return; } if (value < 1 || value > 24) { MessageBox.Show("Please enter a value between 1-24"); } } Validating txtHour by invoking above method
ValidateText(txtHour); Comments
Look into doing your check with the OnKeyPress/OnKeyDown/OnKeyUp events:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress.aspx
Comments
This is a very simplistic implementation but, this will achieve the validation that you are asking for:
int hoursEntered; bool isInteger; isInteger = int.TryParse(txtHour.Text, out hoursEntered); if (hoursEntered < 1 || hoursEntered > 24 && isInteger == true) MessageBox.Show("Your number is either less than 1, greater than 24 or you didn't enter a number", "Warning!", MessageBoxButtons.OK); There are a few things to consider here though. I wrote this code and had it tied to the event handler for a button click. If that is not your implementation, then I would strongly consider using the OnKeyPress/OnKeyUp/OneKeyDown events to do your validation. Otherwise, you can just copy this code into whichever button click event handler you already have in-place.