1

I'm doing a registration form where people can register, in that I will be asking for thier email address's If i could put a regex validation that captures any other emails

example:

xxx@gmail.com, or xxx@ yahoo.com, and only allow xxx@bat.ac.uk i could implement a system that sends an email to the registering person asking them to validate it. I know how to implement the email bit I'm just stuck at this validation part. hope this makes sense

Is there a way to modify the default email validation expression

\w+([-+.']\w+)@\w+([-.]\w+).\w+([-.]\w+)*

huMpty duMpty
  • 14,346
  • 14
  • 60
  • 99
Suits999
  • 369
  • 1
  • 7
  • 25

3 Answers3

1

If this is ASP.NET Web Forms then you can use a RegularExpressionValidator to do the job.

<asp:RegularExpressionValidator ID="SomeID"
    runat="server"
    ControlToValidate="TheControlIDToValidate"
    ErrorMessage="An error message you want to display."
    ValidationExpression="\w+([-+.']\w+)@\w+([-.]\w+).\w+([-.]\w+)*"
    EnableClientScript="true" />

This will validate the value client-side using JavaScript. However, it can be circumvented if JavaScript isn't enabled so server-side you need to do use the Regex class:

Regex rgx = new Regex(@"\w+([-+.']\w+)@\w+([-.]\w+).\w+([-.]\w+)*", RegexOptions.IgnoreCase);
MatchCollection matches = rgx.Matches(input);

then if the matches.Count > 0 you're good.


However, if it's ASP.NET MVC you can use the RegularExpression data annotation. You just add an attribute to the property on your model:

[RegularExpression(@"\w+([-+.']\w+)@\w+([-.]\w+).\w+([-.]\w+)*", "Some error message.")]
public string EmailAddress { get; set; }
Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
0

I advise to use the default validator for checking mail is valid and add a CustomValidator implementing both CLIENT side and Server side validation.

here you can check extensions and perform the custom logic you need.

Stefano Altieri
  • 4,550
  • 1
  • 24
  • 41
  • Finally found the answer to this, Thanks for the help anyway everyone] \w+([-+.]\w+)*@bat.ac.uk That did the job for me – Suits999 Feb 23 '13 at 19:32
0

I would use the built in attribute for validating email address and then in addition to that, check with this regex:

@bat.ac.uk$

Then you know it's a valid email and contains what you're looking for. The validation for email is very complicated and I wouldn't advise rewriting it, so just validate with two different things.

Email address attribute: http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.emailaddressattribute.aspx

CorrugatedAir
  • 809
  • 1
  • 6
  • 16
  • how would u go on to implement this? i tried putting a normal validation to check if the email address is valid and on another custom validation i pasted what u said on the regular expression, but it didn't seem to work, if u can please help me out as I'm stuck on this without being able to progress to the next stage of my development of this webform – Suits999 Feb 21 '13 at 12:39