3

I have try to validate an email like below with jQuery in asp.net mvc like below

<script type="text/javascript">
   function validateEmail(sEmail) {
    var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
    if (filter.test(sEmail)) {
        return true;
    }
    else {
        return false;
    }
      $(document).ready(function() {
   $('#create').click(function() {
        var sEmail = $('#UserName').val();
        if ($.trim(sEmail).length == 0) {
            alert('Please enter valid email address');
            e.preventDefault();
        }
        if (validateEmail(sEmail)) {
            alert('Email is valid');
        }
        else {
            alert('Invalid Email Address');
            e.preventDefault();
        }
    });
});
    });
</script>

While rendering the view i got compilation error Unexpected character '\' in that regex pression.How can i resolve this error?

5
  • you just mixing functions, please sepatate it, document.ready() and validateEmail()
    – Ravi Gadag
    Commented Feb 11, 2013 at 15:25
  • Did you try using the object notation for RegExp, filter = new RegExp(pattern,modifiers); instead of the literal notation filter = /pattern/ Commented Feb 11, 2013 at 15:26
  • I have separated the function and document.ready now i got the same error. Commented Feb 11, 2013 at 15:28
  • The @ symbol is used by the razor view engine, that's probably throwing something off. stackoverflow.com/questions/4690066/…
    – Jack
    Commented Feb 11, 2013 at 15:48
  • Yes.that's the problem. I have put one more @ in that regex expression.Now my problem resolved. Thanks a lot Commented Feb 11, 2013 at 15:58

2 Answers 2

10

I have same error. Here is answer. It works for me. Need use @@ instead of @.

In your case it is:

var filter = /^([\w-\.]+)@@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
0

You've got some weirdness with your braces. Specifically, you didn't close your validateEmail function correctly. Here is the corrected Javascript code:

    function validateEmail(sEmail) {
        var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
        if (filter.test(sEmail)) {
            return true;
        }
        else {
            return false;
        }
    }
    $(document).ready(function() {

        $('#create').click(function() {
            var sEmail = $('#UserName').val();
            if ($.trim(sEmail).length == 0) {
                alert('Please enter valid email address');
                e.preventDefault();
            }
            if (validateEmail(sEmail)) {
                alert('Email is valid');
            }
            else {
                alert('Invalid Email Address');
                e.preventDefault();
            }
        });
    });

EDIT: Also, unrelated, you can make that validateEmail function a bit more succinct since test returns a boolean. See as follows:

    function validateEmail(sEmail) {
        var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;

        return filter.test(sEmail);
    }
5
  • i have tried this but i got the same compilation error.i think the javascript error doesn't generate any compilation error. Commented Feb 11, 2013 at 15:30
  • This is Javascript, no compilation is involved. In fact, I'm not sure why this is tagged ASP.net MVC. This is only a Javascript and jQuery question. Commented Feb 11, 2013 at 15:32
  • Never.this error is generated by .net.that's why compilation error generate at runtime Commented Feb 11, 2013 at 15:34
  • I either think you're misunderstanding something or you're not giving us the whole picture. If you're getting .Net complaining about a backslash, then that's probably because you're not escaping the backslash or something similar. I'm not sure what you are doing that is causing .Net to complain about a backslash when it should be in a JS file or a view, but try adding a backslash before those backslashes in your Regex. Either way, your Javascript code is also invalid and can be fixed with what I posted above. Commented Feb 11, 2013 at 15:37
  • i have tried your code exactly what you have posted.I am using ASP.NET MVC4. Commented Feb 11, 2013 at 15:41

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.