Home > Software design >  Javascript Email Checker in ASP.NET
Javascript Email Checker in ASP.NET

Time:06-22

Basically I am trying to have a text box that upon change it checks if a valid email is in place and if so the form is submitted for the user. I would like to improve the regex too to only allow a specific domain but I can't get any regex to work due to every time I use a @ sign it thinks I am trying to use C# code I looked it up and supposedly adding a : fixes it but it hasn't for me and I tried a ; too.

document.getElementById("txtCallerID").onchange = function () {
        var temp = document.getElementById("txtCallerID").value

        if (/^\w ([\.-]?\w )*@\w ([\.-]?\w )*(\.\w{2,3}) $/.test(temp))
          {
            this.document.submit();
          }
    }

and here is a picture with the error. enter image description here

Have now also tried adding a backslash before the @ to no avail.

enter image description here

CodePudding user response:

You can safely escape the @ symbol with a backslash so it's only recognised as a regex literal. Here is how you would do this...

Change: @ to \@

Fixed regex literal: /^\w ([\.-]?\w )*\@\w ([\.-]?\w )*(\.\w{2,3}) $/

CodePudding user response:

TL;DR To escape the @ sign in cshtml use @@.

For more info, please check this answer.

  • Related