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.
Have now also tried adding a backslash before the @ to no avail.
2
Answers
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})+$/
TL;DR
To escape the
@
sign in cshtml use@@
.For more info, please check this answer.