Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My asp.net 4.0 page has two textboxes for the user to enter his email address.

The first textbox has code behind to validate that the email address is in a valid email address format. After the autopostback from this text box which causes the format validation to occur, focus returns to the first textbox. I would like to set the focus to the second textbox to make sure that a wrong email address was entered in a "Valid" format.

Does anyone have an idea how to do this?

What I have tried:

The following code does not work. It sets the focus, to the next text box but when the autopostback completes the focus returns to the first text box.
C#
Private Sub TextContactEmail_TextChanged(sender As Object, e As EventArgs) Handles TextContactEmail.TextChanged
       Session.Add("LastHadFocus", "TextContactEmail")
       If Not IsValidEmail(TextContactEmail.Text) Then
           lblInvalidEmailAddress2.Visible = True
           TextContactEmail.Text = ""
       Else
           lblInvalidEmailAddress2.Visible = False
           TextCompareContactEmail.Text = ""
           TextCompareContactEmail.Focus()
       End If
   End Sub
Posted
Updated 15-Feb-16 20:33pm
v2
Comments
Sinisa Hajnal 16-Feb-16 2:36am    
Mark the field as invalid and compare it again when second textbox is entered (you have to validate again anyhow to check that two are the same.

BTW, it is not great useability to have to re-type the e-mail. Most people will just copy-paste the one they entered. It is better to just take that mail and send confirmation link to it. That way, you confirm that everything is OK and that e-mail is valid.

Just because something looks like e-mail address doesn't make it so.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900