0

I use this regex for my email validation

/.+@.+\..+/i

I can't get it to work on my server side though. I tried this so far

var pattern = @"^.+@.+\..+i$";
var pattern = @".+@.+\..+i";

I get 0 results trying it (also in my code). Any ideas? Not sure if it helps but the requirement is to allow [email protected] so 1 char, one @ and at least one '.'.

5
  • What does the i in the pattern means? Commented Aug 22, 2014 at 7:18
  • @AvinashRaj to be honest I have no idea :). I got this regex from somebody. I'm really bad at regex. Commented Aug 22, 2014 at 7:19
  • @dejan.s - regex parsing machine are different for javascript and other servers. You need to find the appropriate one. What programming language do you use on server? Commented Aug 22, 2014 at 7:20
  • i means 'Ignore letter case' if you use it in this way: var reg=/[a-z]+/i ,same as var reg=/[a-zA-Z]+/ Commented Aug 22, 2014 at 7:21
  • insensitive casing (/{match}/ I/G (seearch global, insensitive). Commented Aug 22, 2014 at 7:21

3 Answers 3

1

The problem looks to be the i; you don't mean "ends with i" (which is i$), etc - in the javascript, the trailing .../i means "case insensitive" - but there is nothing there to be case sensitive, so just remove the i (from both would be fine, but from the C# at least). You should also remove the ^ and $ that you added from nowhere. For completeness, RegexOptions.IgnoreCase is how you ignore case in .NET Regex.

var pattern = @".+@.+\..+";
Sign up to request clarification or add additional context in comments.

6 Comments

I'll try that out. Would you recommend the ^ and $ in C#? Actually not sure what it does?
@Dejan.S yes; they mean "start" and "end"; they aren't in the javascript version, so the javascript version matches "has this pattern"; with the ^/$, this becomes "is this pattern".
@Dejan.S I need to be careful how I say this, but: if you weren't sure what they did, why did you add them? rather than, say, doing a 2 second google search to find out what they did?
I have no knowledge when it comes to regex. I mean I used it before like got done of the net. Now always seen the ^ $, but the validation before it didn't use it so I just wanted to check. Reason I ask here instead of google is that other can take knowledge from it. Because let's be honest asking this question I did is rookie :)
@MarcGravell while doing validations, it's better to add start and end anchors.
|
1

Just remove the i character from the regex and then add start, end anchors to your pattern,

@"^.+@.+\..+$";

DEMO

3 Comments

Just remove the i and add start,end anchors,
k; I missed the "add"; but why are you adding them? they aren't in the js version...
+1. The other got accepted just becuse of the more extensive answer. Thanks Avinash.
0

JavaScript:

function validateEmail(email) { 
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
}

C#:

bool validateEmail(string email) 
{ 
    var re = new Regex(@"^(([^<>()\[\]\.,;:\s@""]+(\.[^<>()\[\]\.,;:\s@""]+)*)|("".+""))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$") ;
    return re.IsMatch(email);
}

see here :Validate email address in JavaScript?

3 Comments

Before I downvote - OP said the example works in javascript, but doesn't work in C#.
@JoshSmeaton Just Add the C# version :)
I'm not sure "replace everything you have" is always the right answer to "this works for what I want in js; help me get it working in C#".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.