The
CreatedUser
event fires
after the user has been created. That is too late to reject an invalid username.
You
could use the
CreatingUser
event to validate the new username, and set
e.Cancel = true
if it's not valid.
However, it would be far cleaner to use
the RegularExpressionValidator
[
^] to validate the input:
<li>
<asp:Label runat="server" AssociatedControlID="UserName">User name</asp:Label>
<asp:TextBox runat="server" ID="UserName" />
<asp:RequiredFieldValidator runat="server"
ControlToValidate="UserName"
CssClass="field-validation-error"
ErrorMessage="The user name field is required."
/>
<asp:RegularExpressionValidator runat="server"
ControlToValidate="UserName"
CssClass="field-validation-error"
ErrorMessage="Please use your company email ending @company.lc"
ValidationExpression="^[^@]+@company\.lc$"
/>
</li>
protected void RegisterUser_CreatedUser(object sender, EventArgs e)
{
if (Page.IsValid)
{
FormsAuthentication.SetAuthCookie(RegisterUser.UserName, createPersistentCookie: false);
MembershipUser newUser = Membership.GetUser(RegisterUser.UserName);
SendEmail();
string continueUrl = RegisterUser.ContinueDestinationPageUrl;
if (!OpenAuth.IsLocalUrl(continueUrl))
{
continueUrl = "~/";
}
Response.Redirect(continueUrl);
}
}
Regexper: ^[^@]+@company\.lc$
[
^]
Regular Expression Language - Quick Reference[
^]