Subscribe For Free Updates!

We'll not spam mate! We promise.

Jul 26, 2012

Eamil , Phone number and String Validation in Windows Forms

Views:

Eamil , Phone number and String Validation in Windows Forms
Today I show you how to Validate Email address,Phone or mobile numbers and String validation in Winodows froms c#.

As we in asp.net there is a validation control that can easily validate required String.

Here but use regex Expressions for Email Validations .It is Easy and most widely use Technique .


So let start,
first you have to drag drop Text box And Button on Form Designer Surface .

Created a method like below
bool IsvalidEmail(TextBox Email)
        {
                        if (Email.Text.Trim() != "")
            {
 Match rex = Regex.Match(Email.Text.Trim(' '), "^([0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z]"+
        "[-\\w]*[0-9a-zA-Z]\\.)[a-zA-Z]{2,3})$", RegexOptions.IgnoreCase);
                if (rex.Success == false)
                {
                    MessageBox.Show("Please Enter a valid Email-Address ",
                    "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Email.Focus();
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else

            {
                return true;

            }
        }

And Write this Code In OnClick Eent of Button

        private void btShow_Click(object sender, EventArgs e)
        {
            bool Vaild = IsvalidEmail(txtEmail);
            if (Vaild)
            {
                MessageBox.Show("Email address is Correct");
            }
          
        }

Little Description about REGX

Patter   Description
(@)               Match the @ character. This is the first capturing group.
(.+)               Match one or more occurrences of any character. This is the second
              capturing group.
$               End the match at the end of the string.
|(([0-9a-zA-Z]     If the first character is not a quotation mark, match any alphabetic
                            character from a to z or  any numeric character from 0 to 9.

 AND NOW PHONE NUMBER and STRING VALIDATIONS

It is very Simple and Easy you just Write Below code on KEY_Press Event of a textbox.

 private void txtCellNumber_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsNumber(e.KeyChar) && e.KeyChar != (char)Keys.Back && e.KeyChar != '+')
            {
                e.Handled = true;
            }
        }

For String

 private void txtStringValue_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsLetter(e.KeyChar))
            {
                e.Handled = true;
            }
        }

Download Soucre Code

Plese Feel Free to Socializer This Post
SOCIALIZE IT →
FOLLOW US →
SHARE IT →

1 comments:

  1. This is an excellent component for verifying email addresses:
    http://www.kellermansoftware.com/p-37-net-email-validation.aspx

    ReplyDelete

Become a Fan

visual studio learn