2

I currently have an WinForm consisting of Name, Address, Zip Code, State, and Age.

Once the user inputs the data, they click the "Exit" button which confirms no fields are blank, then saves the data to a file. I want to add a Zip Code validation that confirms the textbox (ZipField) contains only numbers.

    private void Btn_Click(object sender, EventArgs e)
    {

        if (String.IsNullOrEmpty(NField.Text) || String.IsNullOrEmpty(AField.Text) ||
            String.IsNullOrEmpty(ZField.Text) || String.IsNullOrEmpty(SField.Text) ||
            String.IsNullOrEmpty(AField.Text))
        {
            MessageBox.Show("Please complete", "Unable to save", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

        return;
        }
        saveInfo();
        Form myForm = Start.getForm();
        myForm.Show();
        this.Close();
    }
HNVDB
  • 39
  • 1
  • 7
  • Why not just stop them from entering in non-numeric values? See [How do I make a textbox that only accepts numbers?](http://stackoverflow.com/questions/463299/how-do-i-make-a-textbox-that-only-accepts-numbers) – Conrad Frix Dec 08 '12 at 05:43

3 Answers3

4

See: http://www.codeproject.com/Articles/13338/Check-If-A-String-Value-Is-Numeric

public bool isNumeric(string val, System.Globalization.NumberStyles NumberStyle)
{
    Double result;
    return Double.TryParse(val,NumberStyle,
        System.Globalization.CultureInfo.CurrentCulture,out result);
}

Edit:

Usage

private void saveAndExitBtn_Click(object sender, EventArgs e)
    {
        if (!isNumeric(custZipField.Text, System.Globalization.NumberStyles.Integer))
        {
            MessageBox.Show("Please enter a valid post code", "Unable to save", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            return;
        }

        if (String.IsNullOrEmpty(custNameField.Text) || String.IsNullOrEmpty(custAddressField.Text) ||
            String.IsNullOrEmpty(custZipField.Text) || String.IsNullOrEmpty(custStateField.Text) ||
            String.IsNullOrEmpty(custAgeField.Text))
        {
            MessageBox.Show("Please complete the entire form", "Unable to save", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

        return;
        }

        //save the data
        saveNewCustomerInfo();
        //next, retrieve the hidden form's memory address
        Form myParentForm = CustomerAppStart.getParentForm();
        //now that we have the address use it to display the parentForm
        myParentForm.Show();
        //Finally close this form
        this.Close();
    }//end saveAndExitBtn_Click method

    public bool isNumeric(string val, System.Globalization.NumberStyles NumberStyle)
    {
        Double result;
        return Double.TryParse(val, NumberStyle,
            System.Globalization.CultureInfo.CurrentCulture, out result);
    }
Zeb Rawnsley
  • 2,210
  • 1
  • 21
  • 33
0

You could, technically, just include a quick validation where you have the rest of them:

try
{
    var zipCode = Convert.ToInt32(custZipField.Text);
}
catch () // Invalid input and such

However, I will recommend you create model class to hold all those properties (name, address, age, zip code, etc.), and have a method called IsValid where you validate all of these and react accordingly.

Edit:

As per Zeb's answer, you could just use TryParse:

int result;
var isNumeric = Int32.TryParse(custZipField.Text, out result);
rae1
  • 6,066
  • 4
  • 27
  • 48
  • 2
    Don't write exception based code. You're better to test for a valid number and then handle either case rather than throwing & catching an exception. – Zeb Rawnsley Dec 08 '12 at 04:15
  • @ZebRawnsley If you need to display an appropiate message based on the error, is better to try and catch for a particular exception. If you don't care, then you can use TryParse. – rae1 Dec 08 '12 at 04:23
  • @rae1n [The general meaning of "an exception is the breaching of predefined assumption of the application". Remember exception and error are not the same.](http://goo.gl/wcTJi) – Zeb Rawnsley Dec 08 '12 at 04:30
  • @ZebRawnsley Like I said, if the OP doesn't care, is better off using TryParse. However, trying to convert a String to a Int is in itself an exceptional case. TryParse actually works the same way except that it swallows the exception being thrown when [parsing the String into Int](http://msdn.microsoft.com/en-us/library/3s27fasw.aspx). – rae1 Dec 08 '12 at 04:42
0

this would do without any try catch.

Int32 zipCode = 0;
Int32.TryParse(custZipField.Text , out zipCode);

If zipCode is zero, then custZipField.Text is empty or not numeric

faheem khan
  • 471
  • 1
  • 7
  • 33