Can I use a regular expression to limit emails to a certain domain?

If you have questions about using mojoPortal, you can post them here.

You may want to first review our site administration documentation to see if your question is answered there.

This thread is closed to new posts. You must sign in to post in the forums.
3/28/2011 5:40:40 PM
Gravatar
Total Posts 18

Can I use a regular expression to limit emails to a certain domain?

I'd like to limit the users of our system to people with email addresses that end in @OURSERVER.com to prevent outsiders from creating user accounts, is there a way to do this with regular expressions? Thanks.

3/29/2011 12:33:11 PM
Gravatar
Total Posts 1203
Proud member of the mojoPortal team

Help support mojoPortal!
Add-on modules

Re: Can I use a regular expression to limit emails to a certain domain?

Give this one a try. I did some minimal validation at this site, but you should double-check and make sure it matches the email addresses you want to limit to.

^[A-Za-z0-9._%+-]+@ourdomain.com$

 

3/29/2011 12:36:59 PM
Gravatar
Total Posts 18

Re: Can I use a regular expression to limit emails to a certain domain?

Where would you use that expression, though? since email is a default property, I don't see any place to put it in the config files... do I have to put it in the source code, or is there a key I can put it in?

3/29/2011 12:57:43 PM
Gravatar
Total Posts 1203
Proud member of the mojoPortal team

Help support mojoPortal!
Add-on modules

Re: Can I use a regular expression to limit emails to a certain domain?

It looks like the email regular expression is hardcoded into register.aspx. You can clone register.aspx to a custom name (like registerme.aspx), and then add this key to your user.config:

<add key="CustomRegistrationPage" value="/path/to/registerme.aspx"/>

Finally, edit registerme.aspx and change the following validator declaration to use your custom email address regex:

<asp:RegularExpressionValidator ID="EmailRegex" runat="server" ControlToValidate="Email"
Display="None" ValidationExpression="^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@(([0-9a-zA-Z])+([-\w]*[0-9a-zA-Z])*\.)+[a-zA-Z]{2,9})$"
ValidationGroup="profile"></asp:RegularExpressionValidator>

Be sure to touch web.config after editing user.config, and test thoroughly.

Hope that works for you.

Jamie

3/29/2011 1:48:11 PM
Gravatar
Total Posts 18439

Re: Can I use a regular expression to limit emails to a certain domain?

Unfortunately I did not get to this in time for the new release coming tomorrow, but I will make it possible in the next release to override our regex expression from web.config, currently it is set from code even though it has an expression declared on it, that gets changed from code.

For now, I would just add another regex validator directly in Register.aspx like this:


<asp:RegularExpressionValidator ID="MyEmailRegex" runat="server" ControlToValidate="Email"
    Display="None" ValidationExpression="yourregex" ErrorMessage="your error message"
    ValidationGroup="profile"></asp:RegularExpressionValidator>

that should work, our validator will still require it to be a valid email address but your additional validator can enforce your domain rule.

Best,

Joe

 

 

 

3/29/2011 1:51:29 PM
Gravatar
Total Posts 18

Re: Can I use a regular expression to limit emails to a certain domain?

Thanks for the feature add! The wait's no problem, I'll just use an extra validator until the new release is out.

3/29/2011 3:34:25 PM
Gravatar
Total Posts 18

Re: Can I use a regular expression to limit emails to a certain domain?

By the way, when the feature is added, will that also have something in the config to allow a custom message to go with the custom regex?

4/4/2011 10:08:08 AM
Gravatar
Total Posts 18439

Re: Can I use a regular expression to limit emails to a certain domain?

Yes, this is now implemented in the source code repository so it will be in the next release. You will be able to add these settings to user.config populated with your regex expression and your error message

<add key="CustomEmailRegex" value=""/>
<add key="CustomEmailRegexWarning" value=""/>

Best,

Joe

4/4/2011 10:11:22 AM
Gravatar
Total Posts 18

Re: Can I use a regular expression to limit emails to a certain domain?

Thanks!

You must sign in to post in the forums. This thread is closed to new posts.