[英]@ symbol causing error with regex when using regex in javascript to validate email address
I'm trying to use javascript and regex to validate emails in my MVC view page. 我正在尝试使用javascript和正则表达式来验证我的MVC视图页面中的电子邮件。
I have the following javascript function: 我具有以下javascript函数:
function (input) {
if (input.attr('name') === "user_name" && input.val() != "") {
var pattern = "\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
return !!input.val().match(pattern);
}
}
I'm using the same sort of function with another regex pattern and that one works but this one seems to be having an issue with the @ symbol. 我在另一种正则表达式模式下使用了相同的函数,并且该函数起作用了,但是这一函数似乎与@符号有关。 with the current pattern I keep getting the error:
使用当前模式,我不断收到错误:
"\\" is not valid at the start of a code block. 在代码块的开头,“ \\”无效。 Only identifiers, keywords, comments, "(" and "{" are valid.
仅标识符,关键字,注释,“(”和“ {”)有效。
I have tried updating the regex to have the part after the @ symbol surrounded by brackets but it still doesnt work. 我尝试过更新正则表达式,使@符号后的部分用方括号括起来,但仍然无法正常工作。 Any ideas how I can get this regex to work to validate emails?
有什么想法可以让我使用此正则表达式来验证电子邮件吗?
I just wanted to post the answer so I can mark my question answered. 我只想发布答案,这样我就可以将问题标记为已回答。 The issue was that I needed to escape the @ in Razor with double @@.
问题是我需要用双@@来代替Razor中的@。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.