javascript 在剃刀页面上使用正则表达式验证电子邮件地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9590176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 07:09:10  来源:igfitidea点击:

validating email address using regular expression on razor page

javascriptregexrazor

提问by COLD TOLD

Hi I am using razor and trying to use regular expression to validate email address here the validation function

嗨,我正在使用剃刀并尝试使用正则表达式来验证电子邮件地址这里的验证功能

function validateEmail(txtEmail){
   var a = document.getElementById(txtEmail).value;
   var filter = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{0,4}$/;
    if(filter.test(a)){
        return true;
    }
    else{
        return false;
    }
}?

but since the regular expression has a @ sign razor thinks it part of it syntax and gives me an error.

但是由于正则表达式有一个@ 符号,razor 认为它是它语法的一部分,并给了我一个错误。

Is there anyway to avoid to make sure razor disregards @ sign in JavaScript

无论如何要避免确保剃刀无视 @ 登录 JavaScript

Thanks.

谢谢。

采纳答案by bbedward

Unicode may work like this

Unicode 可能像这样工作

string filter = "/^[a-zA-Z0-9_.-]+\u0440[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{0,4}$/";

Alternatively in razor @@ is a normal @ symbol, it should work in your javascript.

或者在剃刀中 @@ 是一个普通的 @ 符号,它应该在你的 javascript 中工作。

string filter = "/^[a-zA-Z0-9_.-]+@@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{0,4}$/";

回答by demir

try this may be help you

试试这可能对你有帮助

function validateEmail(email) { 
    var re = /^[_a-z0-9-A-Z-]+(\.[_a-z0-9-A-Z-]+)*@[a-z0-9-A-Z-]+(\.[a-z0-9-A-Z-]+)*(\.[a-z]{2,4})$/;
    return re.test(email);
} 

回答by Mohamed Rasik

It is common to check the format of the email is valid or not. To validate email address we need to use regular expression. In MVC razor we should use @@ symbol to perform validation. MVC razor:

检查电子邮件的格式是否有效是很常见的。要验证电子邮件地址,我们需要使用正则表达式。在 MVC razor 中,我们应该使用 @@ 符号来执行验证。 MVC剃须刀:

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

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

Normal Html:

普通 Html:

For normal we should use @ symbol to perform validation

对于正常情况,我们应该使用 @ 符号来执行验证

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

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

for more details visit

欲了解更多详情,请访问