In our day to day life, developers need to frequently process data and text. The endusers input the data and we need to validate that input data according to our business rules.
A regular expression is a set of characters that can be compared to a string to determine whether the string meets specified format requirements. You may choose to replace it with some other characters or perform some other tasks based on the results obtained. So today i will tell u how to validate email using regular expressions in c#.net.
5. Go to the events of textbox and double click on the leave event:
6. Write the code listed below:
//Add one namespace
using System.Text.RegularExpressions;
private void textBox1_Leave(object sender, EventArgs e)
{
Regex reg = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
if (!reg.IsMatch(textBox1.Text))
{
MessageBox.Show("Invalid email");
textBox1.Focus();
}
else
{
MessageBox.Show("Valid Email");
}
}
The regular expression engine in the .NET Framework is represented by the Regex class. The Regex.IsMatch method returns true if the string matches the pattern, or false if it does not. The IsMatch method is used to check that whether the email entered by user is valid or invalid in the above code.
7. Press F5 to execute this application.
A regular expression is a set of characters that can be compared to a string to determine whether the string meets specified format requirements. You may choose to replace it with some other characters or perform some other tasks based on the results obtained. So today i will tell u how to validate email using regular expressions in c#.net.
- Open Visual C# 2010 Express
- Then click on File-->New Project
- A dialog box will open, then choose windows forms application and click OK.
- After that design the form listed below:
5. Go to the events of textbox and double click on the leave event:
6. Write the code listed below:
//Add one namespace
using System.Text.RegularExpressions;
private void textBox1_Leave(object sender, EventArgs e)
{
Regex reg = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
if (!reg.IsMatch(textBox1.Text))
{
MessageBox.Show("Invalid email");
textBox1.Focus();
}
else
{
MessageBox.Show("Valid Email");
}
}
The regular expression engine in the .NET Framework is represented by the Regex class. The Regex.IsMatch method returns true if the string matches the pattern, or false if it does not. The IsMatch method is used to check that whether the email entered by user is valid or invalid in the above code.
7. Press F5 to execute this application.
No comments:
Post a Comment