-1

I want to use regex patterns in the answers provided to this question How to validate an email address in JavaScript?

but all of them show red squiggly line unde at sign @. What's the reason and how to solve this problem?

/^(([^<>()\[\]\\.,;:\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,}))$/

This is my js function:

<!--language: lang-js-->
$('input.filled-in').change(function () {
                check_bname(this, $(this).next().next().children().first());
            })

            function check_bname(input, errorMsg) {

                var  patternEmail = /^(([^<>()\[\]\\.,;:\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,}))$/,
                    $elemInput = $(input),
                    $elemError = errorMsg,
                    $inputVal = $(input).val(),
                    attrName = $elemInput.attr("id");

                    if($elemInput.hasClass("filled-in"))
                    {
                        patternMatch = $inputVal !==''&& patternEmail.test($inputVal);

                    }


                    $elemError[patternMatch ? 'hide' : 'show']();

                    }

                  if (!patternMatch) {


                    if($elemInput.hasClass("filled-in"))
                    {

                        $elemError.html($inputVal === '' ? "Should not be empty" : "Use valid form of Email Address e.g (example@example.com)");

                }
            }
Cavid
  • 125
  • 13
  • What exactly does this line do/start `patternEmail =` ? Because there are a couple of double quotes, which is ok if this is a regex object like `var rx = /asdf/;` But, it looks like a list of some kind. –  Jul 13 '19 at 19:34
  • @I'm not sure what you mean by `do/stat` line . `patternEmail` is a variable and other stuff below different variables separated by comma – Cavid Jul 13 '19 at 19:41
  • Does JS allow the Dollar sign as a variable name ? –  Jul 13 '19 at 20:00
  • The bottom line is your regex object `/^(([^<>()\[\]\\.,;:\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,}))$/` parses correctly, so something else around it is causing a problem. You can isolate a single line where you just assign the regex object and nothing else. If you don't have an error, build some code around it until you identify it. If you do have an error, stop and don't do anything. But, don't just plop down a bunch of code and claim the regex is in error man, don't do that ................ –  Jul 13 '19 at 20:09
  • as I said it looks like `@` sign is causing the problem, when I remove this i don't have compilation error. Is there any chracacter set in regex that replaces the `@` sign? – Cavid Jul 13 '19 at 20:40
  • hey bud, you didn't try my suggestion .. _as I said_ –  Jul 13 '19 at 23:56
  • @sln tried and to no avail.. I also use different reg expressions inside this function and they work without a problem. Only this one cause the problem – Cavid Jul 14 '19 at 12:13

2 Answers2

0

The regex is OK. This must be wrong syntax checking. Many IDEs show warnings or errors when using regex in this form in JavaScript. You can supress the warning or change the regex like this:

var patternEmail = new RegExp('^(([^<>()\\[\\]\\.,;:\\s@\\"]+(\\.[^<>()\\[\\]\\.,;:\\s@\\"]+)*)|(\\".+\\"))@(([^<>()[\\]\\.,;:\\s@\\"]+\\.)+[^<>()[\\]\\.,;:\\s@\\"]{2,})$','i'); patternEmail.test($inputVal);

Note: \ have to be escaped in this form. Use \\ instead

I would supress the warning because the other form is better in this situation.

What IDE is showing the red squiggly line?

rmac38
  • 123
  • 7
0

I do not see any problem with any characters in your regex.
This JS sample uses your regex.

function getPattern()
{
     var  pattern = /^(([^<>()\[\]\\.,;:\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 pattern;
}

var patn = getPattern();
var estr = "www.google.com";
var match;

if ( match = estr.match( patn ) )
     console.log( match[0] );
else
  console.log( "No match" );
  • 1
    I moved the code into a js file type and it worked. Looks like using regex inside cshtml is something has to be avoided becaue `@` char might conflict with the razor signs - `@` that cshtml template uses – Cavid Jul 14 '19 at 20:09