B
Ben Amada
Hi all. I know very little about regular expressions, but wanted to use one
to validate an email address a user would be entering before the form is
submitted. There are many examples out there. Two two functions I found
below both validate an email address correctly, but work differently. The
first one uses the "search" method of a string, and the second uses a RegExp
object. I'm just wondering which method might be most reliable across the
most number of javascript enabled browsers out there ... the string.search()
method or the RegExp() object?
Any insight is appreciated, thanks!
Ben
function checkOne(sEmail) {
if
(sEmail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)
!= -1) {
return true;
} else {
return false;
}
}
function checkTwo(sEmail) {
var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
var regex = new RegExp(emailReg);
return regex.test(sEmail);
}
to validate an email address a user would be entering before the form is
submitted. There are many examples out there. Two two functions I found
below both validate an email address correctly, but work differently. The
first one uses the "search" method of a string, and the second uses a RegExp
object. I'm just wondering which method might be most reliable across the
most number of javascript enabled browsers out there ... the string.search()
method or the RegExp() object?
Any insight is appreciated, thanks!
Ben
function checkOne(sEmail) {
if
(sEmail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)
!= -1) {
return true;
} else {
return false;
}
}
function checkTwo(sEmail) {
var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
var regex = new RegExp(emailReg);
return regex.test(sEmail);
}