0

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

  • Are you using visual studio? and in what type of file is this saved? – MrJami Jun 21 '22 at 14:00
  • Yes I am using visual studio to run / edit sometimes with a mix of visual studio code on editing. This is a ASP.net website and this is currently in the main index.cshtml view. – MystifiedSky Jun 21 '22 at 14:18

2 Answers2

0

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})+$/

squidee_
  • 120
  • 9
0

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

For more info, please check this answer.

Andres A.
  • 1,329
  • 2
  • 21
  • 37