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.
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}$/";
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);
}
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
Related
I am using jquery validation to validate my form.
I have some url validation required for this
I have one field in which I want to validate url
validation should be such that it should contain "phobos.apple.com, deimos3.apple.com, albert.apple.com, gs.apple.com, itunes.apple.com, ax.itunes.apple.com"
any of these url in it.
if the entered string contain any of above url than it must validate it.
using jquery validation or javascript.
I would check the url with a regular expression like this:
function checkURLValue(url) {
// this regex would not match a leading protocol which you actually should prepend..
// you should definitely look for a better regular expression
var regEx = new RegExp('^[a-z0-9\\.]+\\.[a-z0-9]+$');
if (regEx.test(url)) {
return true;
}
return false;
}
I hope this helps!
How would I check that a user's email address ends in on of the three
#camel.com, #mel.com, #camelofegypt.com
This is my code so far, it just validates whether or not the user has provided any text
if ($.trim($("#email").val()).length === 0) {
alert('You must provide valid input');
return false;
I want to implement the email address validation into my code.
use the following regex:
var reg=/#(camel|mel|camelofegypt)\.com$/
reg.test(email)
This only validates if you email ends in one of the three above. If you want ot know general email validation, search the web. There are tons of those
As described in the library to regular expressions, it is difficult to truly validate an email address. However, the below taken from the above website will do a good job.
The official standard is known as RFC 2822. It describes the syntax that valid email addresses must adhere to. You can (but you shouldn't--read on) implement it with this regular expression:
(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")#(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])
Simple Regex:
\b[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}\b
Trade offs of validating email addresses:
Yes, there are a whole bunch of email addresses that my pet regex doesn't match. The most frequently quoted example are addresses on the .museum top level domain, which is longer than the 4 letters my regex allows for the top level domain. I accept this trade-off because the number of people using .museum email addresses is extremely low. I've never had a complaint that the order forms or newsletter subscription forms on the JGsoft websites refused a .museum address (which they would, since they use the above regex to validate the email address).
However, if you just want your specific domain this is definitely a possibility but it is not recommended to deny an email address because it fails these regular expressions.
Taking the above you could simply validate using the following Regex:
\b[A-Z0-9._%+-]+#(camel|mel|camelofegypt)\.com\b
or:
^[A-Z0-9._%+-]+#(camel|mel|camelofegypt)\.com$
The difference between these two regex are simple, the first regex will match an email address contained within a longer string. While the second regular expression will only match if the whole string is the email address.
JavaScript Regex:
/\b[A-Z0-9._%+-]+#(camel|mel|camelofegypt)\.com\b/i
or:
/^[A-Z0-9._%+-]+#(camel|mel|camelofegypt)\.com$/i
Special Note: You should likely allow for case insensitive with your regex using the i parameter since John#CAMEL.com is the same as john#camel.com. Which i've done in the above regex.
function ValidateEmail(email) {
var regex = /^([a-zA-Z0-9_\.\-\+])+\#(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
return regex.test(email);
}
OR
This is a regular-expression email validation comparison that will test a bunch of valid/invalid email address against the regex provided by you in the textarea below.
^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))#((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$
Try out this javascript
function IsEmail(email) {
var regex = /^([a-zA-Z0-9_\.\-\+])+\#(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
return regex.test(email);
}
Try this function,
function validateEmail($email) {
var emailReg = /^([\w-\.]+#([\w-]+\.)+[\w-]{2,4})?$/;
if( !emailReg.test( $email ) ) {
return false;
} else {
return true;
}
}
if( !validateEmail(email)) { /* do stuff here */ }
var x=$("#email").val();
var n=x.split("#");
if($.inArray(camel.com,n)!= -1)//checks if camel.com is there in your email address if not the value will be -1 #is not given in front of camel.com because its split after # which means that #will be present before it if camel.com is prescent.
{
}
else
if($.inArray(mel.com,n)!= -1)
{}
else
if($.inArray(camelofegypth.com,n)!= -1)
{}
else
alert("");
using new regex
demo Here
added support for Address tags (+ sign)
function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))#((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
return pattern.test(emailAddress);
};
Note : Keep in mind that no 100% regex email check exists
I am trying to check the e-amil address like abc#example.co.uk or a.bc#eyample.com etc
and I am using regular expression like
[a-zA-Z0-9\._]+#[^.]+[a.zA-Z]+\.[a-z{2,5}]+
Can someone suggest how to correct it?
Thank you
See Using a regular expression to validate an email address
and you can try :
[a-zA-Z0-9.!#$%&'*+-/=?\^_`{|}~-]+#[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*
This should work
Example
var sEmail = txtEmail;
var filter = /^([\w-\.]+)#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
if (filter.test(sEmail)) {
return true;
}
else {
return false;
}
If you make an input type email in new browsers it will validate to a certain level. It will only look for characters followed by a # and than a few characters after it.
Validating is to catch user mistakes not to make it harder for users to fill in your form.
I want to replace apostrophes "'" with "/" from string.
for example: var str ="te'xt";
want output like this "te/xt"
I'm trying to use a regular expression validation using .match for email validation and for some reason it only shows invalid even though all formats are followed!
javascript function
function check_email_valid(emails) {
var emailRegex = '^[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$';
if (emails.match(emailRegex)) {
jQuery('#<%=Label16.ClientID%>').css('color', 'green');
jQuery('#<%=Label16.ClientID%>').show();
jQuery('#<%=Label16.ClientID%>').text("Valid Email!");
}
else {
jQuery('#<%=Label16.ClientID%>').css('color', 'red');
jQuery('#<%=Label16.ClientID%>').show();
jQuery('#<%=Label16.ClientID%>').text("Invalid Email!");
}
Event trigger
$('#<%=TextBox8.ClientID%>').keyup(function () {
var email = jQuery('#<%=TextBox8.ClientID%>').val();
check_email_valid(email);
});
I keyed in test#mail.com and got "Invalid Email!". Any idea why?
Case-sensitivity.... And validating email addresses is a bit more complicated then that... (.museum for one, but there is more..).
JavaScript has a slightly different way of defining regex patterns than other languages. They're not strings, but instead simply start with a slash, end with one, and after that you can specify some flags.
Your pattern is defined like this:
var emailRegex = /^[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
The i flag at the end of the pattern makes it case insensitive.
Can't believe how difficult this seems to be all I want to is to validate a user inout using javascript to make sure that it is an email address. But can't get it to work:
I am using:
//validates a regulaer expression
Utilities2.prototype.validateEmail = function(stringToValidateArg)
{
alert('about to check regexp');
var regExpPattern = /^[a-zA-Z0-9._-]+#[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
alert(regExpPattern.test(stringToValidateArg));
}
But this always returns false, any ideas why is it because of the regular expression?
The regular expression that I'm using is
/([\w-\.\+]+\#[\w-]+\.+[\w]{2,4})/gi
Try this one, should be a bit simpler :)