Click here to Skip to main content
15,899,313 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more: , +
SQL
can i use same regex in c# that i have used in javascript


like:-
C#
private static bool IsValidEmail(string senderEmail)
{
    var regExPattern_forEmail = new Regex(@"^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$");
    return (string.IsNullOrEmpty(senderEmail) != true && regExPattern_forEmail.IsMatch(senderEmail) != true);
}


C#
function validateEmail(objEmail)
{
    var regExPattern_forEmail = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;

    if (regExPattern_forEmail.test(objEmail.value) == false)
    {
        alert('\"' + objEmail.value + '\" is INVALID e-mail Address');
        return false;
    }
    else
    {
        alert('\"' + objEmail.value + '\" is VALID e-mail Address');
        return true;
    }

}
Posted
Comments
kumarsaurav32 25-Jul-13 14:02pm    
my question is that:
i have
var regExPattern_forEmail = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/; that i have used in my javascript, can i use this same regex in the code behind like:-
var regExPattern_forEmail = new Regex(@"^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$");
Matt T Heffron 25-Jul-13 16:50pm    
See the Solutions below for your specific question, however it appears that top level domains can be longer than 4 characters: http://en.wikipedia.org/wiki/Top-level_domain you might want to revise the regex...

In most cases, yes. I believe C# does support some extra syntax that JavaScript may not, but it doesn't look like you're using any of that. The only way to know for sure is to try it out though. Keep in mind certain options may have different defaults for regexes in different languages though (for example, multiline matching).
 
Share this answer
 
For C# the regex you should use to validate the email is the first regex that you have.
Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
 
Share this answer
 
v2
Comments
[no name] 26-Jul-13 11:26am    
why everybody down vote their is nothing wrong with the answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900