ASP Forum
Got a brain teaser...maybe not for GURUS :)
bbcompent1 | Posted 6:14am 28. October 2008 Server Time |

Ok, here is what I'm trying to do.  I have a basic email form and what I'd like to do using standard regex is block email addresses like hotmail.com, gmail.com and so on from being entered.  I've been trying a miriad of different things but nothing works properly.  I'm using asp.net regular validation.  What I have so far is this:

<asp:RegularExpressionValidator id="regExEmailAdd" runat="server" CssClass="ERROR_FORM_VALIDATION" ErrorMessage="Invalid email address" Display="Dynamic" ControlToValidate="User_Email_Address" ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">Invalid email address</asp:RegularExpressionValidator>
stimpy | Posted 4:25pm 29. October 2008 Server Time |

Why not just use a custom validator and then on the back end check to see if it has a hotmail or gmail suffix and if so throw the error?
stimpy | Posted 5:05pm 29. October 2008 Server Time |

So here's the custom validator:
<asp:CustomValidator id="CustomValidator1" runat="server"
        ControlToValidate="Text1"
        OnServerValidate="ServerValidate"
        Display="Static">
Email address cannot end in '@hotmail.com' or '@gmail.com.' Please enter a valid email address.
</asp:CustomValidator>

and then here's the aspx.cs:

void btnEmailSubmit_Click(object sender, EventArgs e)
{
     if(Page.IsValid)
     {
       // Do something
     }
     else
     {
       // Do something else
     }
}

void ServerValidate (object source, ServerValidateEventArgs value)
{
   // use the indexof function to look for your strings of '@gmail.com' and '@hotmail.com
  // indexof returns a -1 if the string is not found
  if((txtEmail.Text.IndexOf("@hotmail.com") = -1) || ((txtEmail.Text.IndexOf("@gmail.com")))
{
     return false;
}
else
{
     return true
}

THis may not be EXACTLY the way but it should put you on the right path. I haven't tested this code so no promises it runs out of the box....
}
stimpy | Posted 5:06pm 29. October 2008 Server Time |

Ooops got the return false and the return trues switched around...
bbcompent1 | Posted 12:31am 7. November 2008 Server Time |

Its ok, I figured it out.  I put it up on one of the regex forums, maybe regexfinder.com?


Reply to Post Got a brain teaser...maybe not for GURUS :)



Back to Forum Page