Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
4.33/5 (5 votes)
I am using javascript to validate email but its giving Parsing error in regex

JavaScript
var regex = new RegExp('^[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*@[a-z0-9]+([_|\.|-]­{1}[a-z0-9]+)*[\.]{1}(com|ca|net|org|fr|us|qc.ca|gouv.qc.ca)$', 'i');


Parser Error Message: "[" is not valid at the start of a code block. Only identifiers, keywords, comments, "(" and "{" are valid.

Plz help me to solve
Thanks
Posted
Updated 26-Jan-14 21:59pm
v3
Comments
Graham Breach 23-Jan-14 6:10am    
I've tried your regex in Chrome, Firefox and IE, and all three browsers are happy with it. Where did the error message come from?
[no name] 23-Jan-14 6:12am    
really ????? how man, I am using chrome..
[no name] 23-Jan-14 6:13am    
Same error in ff
Graham Breach 23-Jan-14 6:20am    
I just pasted the line into the Javascript console, then used regex.exec() to test a couple of strings.

Are you sure this is being executed as Javascript, and not on the server?

Email Validation Expression : ^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*@[a-z0-9-]+(\\.[a-z0-9]+)*\\.([a-z]{2,4})$
 
Share this answer
 
you can use below syntax for email regular expression validation in jquery:

var regx = /[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/igm;
if(regx .test(email here)){ //do something }
 
Share this answer
 
Email Validation Expression : \w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*

Hope this would help you !! :)
 
Share this answer
 
try this one

C#
 <script type="text/javascript">
        function validateCaseSensitiveEmail() {

            var email = document.getElementById('txtEmail').value;
            var reg = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/;
            if (reg.test(email)) {
                alert("Valid");
            }
            else {
                alert("In Valid");
            }
        } 
</script>
 
Share this answer
 
Try below code to validate your email:

function validatetoemail(my_email) {
var filter = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/;
if (filter.test(my_email)) {
return true;
}
else {
return false;
}
}

hope it will work also for you.
 
Share this 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