0

Email validation jquery mvc function in this code is not working..

var CompanyName = $("#txtCompanyName").val();
            var Email = $("#txtEmail").val();
            var Country = $("#Country").val();

            if (CompanyName.length == 0) {
                alert("textbox value can't be empty");
                document.getElementById('txtCompanyName').style.border = "solid 1px red";
                $("#txtCompanyName").focus();
            }
            else if (Email.length == 0) {
                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");
                }
            }

            else if (Country.length == 0) {
                alert("Country value can't be empty");
                document.getElementById('Country').style.border = "solid 1px red";
                $("#Country").focus();
            }
Ivin Raj
  • 3,448
  • 2
  • 28
  • 65
  • Beside that you have two `@`, should think about just using something like `/^\S+@\S+$/`. Your RegExp does not accept many valid email addresses. E.g. `someone@example.email` or `some+info@example.com` and all those international domains that have to be encoded as punycode. – t.niese Sep 21 '17 at 13:35
  • is there any way to get validation @t.niese – Ivin Raj Sep 21 '17 at 13:38
  • You might check one of the answers to [How to validate email address in JavaScript?](https://stackoverflow.com/questions/46155) e.g. [this answer](https://stackoverflow.com/a/44671643/1960455) – t.niese Sep 21 '17 at 13:41
  • Possible duplicate of [How to validate email address in JavaScript?](https://stackoverflow.com/questions/46155/how-to-validate-email-address-in-javascript) – t.niese Sep 21 '17 at 13:41

1 Answers1

0

two ampersand symbols is the problem.

var reg = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/;
  • "[" is not valid at the start of a code block. Only identifiers, keywords, comments, "(" and "{" are valid. – Ivin Raj Sep 21 '17 at 13:30