473,405 Members | 2,334 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

How to give email validation inside a textbox?

I have the following program (C# VS 2005) for validating an email id entered in a textbox.
It works fine.

Namespace used -
using System.Text.RegularExpressions;


At present I have written the functionality inside textBox1_Validating event as shown below and when I click button1(my form contains button control called button1), it is giving me the perfect output.

Expand|Select|Wrap|Line Numbers
  1. private void textBox1_Validating(object sender, CancelEventArgs e)
  2.         {
  3.             System.Text.RegularExpressions.Regex rEMail = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z][\w\.-]{2,28}[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");
  4.             if (textBox1.Text.Length > 0)
  5.             {
  6.                 if (!rEMail.IsMatch(textBox1.Text))
  7.                 {
  8.                     MessageBox.Show("E-Mail expected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  9.                     textBox1.SelectAll();
  10.                     e.Cancel = true;
  11.                 }
  12.             }
  13.         }
When I clicked button1, it gave me the required output.


But sir, I want some slight change.

1. I again dragged and dropped button2, button3 into my form.
Now if I click either button1 or button2 or button3 it is showing the massage box. This should be restricted. ie the error message should be shown only on the click of button1(ie my save button) and not on the click of other buttons.

2. after entering the mail id in the textbox and if I press the enter key, then it should show me the same error message box

3. if the textbox is empty and if I press enter key or button1(my save button)then also it should prompt me to enter an email id by showing the same message box.

Please help me telling what pieces of code I must write inside the following event handlers
1.
Expand|Select|Wrap|Line Numbers
  1. private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
  2.    {
  3.    }    
2.
Expand|Select|Wrap|Line Numbers
  1. private void textBox1_Validating(object sender, CancelEventArgs e)
  2.    {
  3.  
  4.    }
3.
Expand|Select|Wrap|Line Numbers
  1. private void button1_Click(object sender, EventArgs e)
  2.    {
  3.    }
I am not sure whether all the above three event handlers are required , what all pieces of code I must write in each event handler or any additional event handlers are required(if so, what code I must write inside them ) to achieve the above mentioned functionality
Please help me.
Thanks and Regards,
-jm
Jan 21 '08 #1
1 2605
Shashi Sadasivan
1,435 Expert 1GB
Hi jm,
You dont need to paste the same code in all the event handlers (but the compiler would do it)

create a method which validates the email address, and on each even (textBox_validating, button_click, etx) call that method.

Cant get more difficult than that
Jan 21 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: A.M | last post by:
Hi, I have a validation control in my page that upon any invalid data, it disables all buttons in the page. basicly i don't have any postback in the page if the validator finds any error. How...
1
by: Bryan | last post by:
I trying to figure out out to validate a textbox inside a repeater once a person presses a button on a repeater. Here's the validation Code behind: Please be aware that line e.Item.ItemIndex...
2
by: Xendor | last post by:
Hi, i am a noobie in asp.net and i have the following problem. I use an Form with a textbox to enter a mail-adress. In this textbox a valid email-adress should be entered. But when i leave the...
1
by: Hong Hao | last post by:
Recently, I was trying to modify an existing aspx page when client-side validation on that page stopped working. I searched this group and the web in general and found that other people have had...
2
by: Tim Frawley | last post by:
Source code attached indicates my problem with validation and a button bar save button. Fill the Textbox with some text then tab off the control. The message box will display the text in the...
1
by: KenLee | last post by:
I use asp 2.0 net. what is the same for "onblur" in asp control such as "<asp:textbox / >"? I use "<asp:textbox />" inside Gridview. I try to validation using client side. when I try using ...
12
by: Dabbler | last post by:
I need to insure that at least one of three phone number fields has a value (requiredfield) but I'm not sure of a way to implement this without server side logic. Is there a way to use the...
3
by: mark4asp | last post by:
How can I stop my Calendar control from firing the form validation events? I have a form containing several controls which have several validation controls each. One control is a TextBox...
2
by: =?Utf-8?B?SmVycnkgSg==?= | last post by:
Hello, I am using asp.net 2003 using C#. I am creating textboxes dynamically and want to validate to make sure the user can only input an integer. I don't think I can use the validators...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.