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 .
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
|(([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.
first you have to drag drop Text box And Button on Form Designer Surface .
Created a method like below
bool IsvalidEmail(TextBox Email)Little Description about REGX
{
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");
}
}
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. |
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)For String
{
if (!char.IsNumber(e.KeyChar) && e.KeyChar != (char)Keys.Back && e.KeyChar != '+')
{
e.Handled = true;
}
}
private void txtStringValue_KeyPress(object sender, KeyPressEventArgs e)Download Soucre Code
{
if (!char.IsLetter(e.KeyChar))
{
e.Handled = true;
}
}
This is an excellent component for verifying email addresses:
ReplyDeletehttp://www.kellermansoftware.com/p-37-net-email-validation.aspx