validating email address using regular expression

2019-04-06 02:44发布

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.

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

Thanks.

3条回答
姐就是有狂的资本
2楼-- · 2019-04-06 03:19

Unicode may work like this

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.

string filter = "/^[a-zA-Z0-9_.-]+@@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{0,4}$/";
查看更多
劳资没心,怎么记你
3楼-- · 2019-04-06 03:21

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);
} 
查看更多
Animai°情兽
4楼-- · 2019-04-06 03:26

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:

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:

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

for more details visit

查看更多
登录 后发表回答