Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Using regular expressions in ASP to validate an email address

regular expressions string validation

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1 getreal720

getreal720

    CC Newcomer

  • Member
  • PipPip
  • 13 posts

Posted 07 February 2014 - 11:02 AM

I'm working towards a solution that requires me to write a web service that validates if a string is a valid government or military email address.

 

This is only going to validate the format of the text entered and not that it actually exists. From that end, I have figured out that I can use regular expressions to do so. However, it seems like I'm having difficulty wrapping my head around setting up the regular expression.

 

The following code snippit is an example that I've found 

<%@ language="C#" %>
<form id="form1" runat="server">
    <asp:TextBox ID="txtName" runat="server"/>
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" />
    <asp:RegularExpressionValidator ID="regexpName" runat="server"     
                                    ErrorMessage="This expression does not validate." 
                                    ControlToValidate="txtName"     
                                    ValidationExpression="^[a-zA-Z'.\s]{1,40}$" />
</form>

I see where it says "ValidationExpression" and I've seen some other examples but I just cant figure out how to change the argument of that command to validate for say ValidationExpression="@mail.mil" for example... so that I can verify the formatting of the email address?



#2 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts

Posted 07 February 2014 - 11:55 AM

For the regular expression, check here: http://regexlib.com/...CookieSupport=1

 

Use the regular expression validator to verify it is a valid email address.

 

You can use a custom validator, and have a list of valid email domains in a database/file/etc, and match the domain part of the email address to the list.  If the domain exists in the list, let it pass.  


My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth