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.
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.
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.
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.
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}$/";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With