Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

validating email address using regular expression on razor page

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.

like image 569
COLD TOLD Avatar asked Mar 06 '12 19:03

COLD TOLD


People also ask

Can email be validated with regex?

Regex provides the ability to validate the structure of an email address. It can be handled with one or two lines of code and can easily be tweaked to handle a wide variation of different parameters. However, one thing to keep in mind is that it can only check the structure of an email address.

How do you validate a regular expression?

When you create a text question, along with word/character limits, you can validate for Regex pattern matching. To validate a field with a Regex pattern, click the Must match pattern check box. Next, add the expression you want to validate against. Then add the message your users will see if the validation fails.

Which method is used to check the validation of a email address?

The IsValidEmail method then calls the Regex. IsMatch(String, String) method to verify that the address conforms to a regular expression pattern. The IsValidEmail method merely determines whether the email format is valid for an email address; it doesn't validate that the email exists.


1 Answers

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}$/";
like image 94
bbedward Avatar answered Sep 23 '22 23:09

bbedward