Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing something for a company and the user must register using the company's email address. "someName@company.lc"

I am using the default asp.net 2012 Account Registration.

I want when the user put information in the textbox id=username, that if it does not contain @Company.lc to send a message to them telling to use the correct email address
and give them the oppunitity to change it.

At present it shows the message but continue continue executing.

What I have tried:

<WizardSteps>
           <asp:CreateUserWizardStep runat="server" ID="RegisterUserWizardStep">
               <ContentTemplate>


                   <p class="message-info">
                       Passwords are required to be a minimum of <%: Membership.MinRequiredPasswordLength %> characters in length.
                   </p>

                   <p class="validation-summary-errors">
                       <asp:Literal runat="server" ID="ErrorMessage" />
                   </p>

                   <fieldset>
                       <legend>Registration Form</legend>
                       <ol>
                           <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." />
                           </li>
                           <li>
                               <asp:Label runat="server" AssociatedControlID="Email">Email address</asp:Label>
                               <asp:TextBox runat="server" ID="Email" TextMode="Email" />
                               <asp:RequiredFieldValidator runat="server" ControlToValidate="Email"
                                   CssClass="field-validation-error" ErrorMessage="The email address field is required." />
                           </li>
                           <li>
                               <asp:Label runat="server" AssociatedControlID="Password">Password</asp:Label>
                               <asp:TextBox runat="server" ID="Password" TextMode="Password" />
                               <asp:RequiredFieldValidator runat="server" ControlToValidate="Password"
                                   CssClass="field-validation-error" ErrorMessage="The password field is required." />
                           </li>
                           <li>
                               <asp:Label runat="server" AssociatedControlID="ConfirmPassword">Confirm password</asp:Label>
                               <asp:TextBox runat="server" ID="ConfirmPassword" TextMode="Password" />
                               <asp:RequiredFieldValidator runat="server" ControlToValidate="ConfirmPassword"
                                    CssClass="field-validation-error" Display="Dynamic" ErrorMessage="The confirm password field is required." />
                               <asp:CompareValidator runat="server" ControlToCompare="Password" ControlToValidate="ConfirmPassword"
                                    CssClass="field-validation-error" Display="Dynamic" ErrorMessage="The password and confirmation password do not match." />
                           </li>
                       </ol>
                       <asp:Button runat="server" CommandName="MoveNext" Text="Register" />
                   </fieldset>
               </ContentTemplate>
               <CustomNavigationTemplate />




protected void RegisterUser_CreatedUser(object sender, EventArgs e)
        {
                               
            FormsAuthentication.SetAuthCookie(RegisterUser.UserName, createPersistentCookie: false);

            MembershipUser newUser = Membership.GetUser(RegisterUser.UserName);
            //Make sure that the user have company email name@company.lc

            if (newUser.Email.Contains("@company.lc") != true)
            {
                MsgBox("Please used your Company email ending @company.lc ");
                            }
            else if (newUser.Email.Contains("@company.lc") == true)
            {
                SendEmail();
                string continueUrl = RegisterUser.ContinueDestinationPageUrl;
                if (!OpenAuth.IsLocalUrl(continueUrl))
                {
                    continueUrl = "~/";
                }
                Response.Redirect(continueUrl);
            }
        }
        public void MsgBox(String msg)
        {
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language='javascript'> alert('" + msg + "')</script>");
        }
Posted
Updated 22-Mar-17 11:01am
v2

1 solution

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>

C#
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[^]
 
Share this answer
 

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