With JavaScript I want to take a input 1st validate that the email is valid (I solved for this) 2nd, validate that the email address came from yahoo.com
Anyone know of a Regex that will deliver the domain?
thxs
var myemail = '[email protected]'
if (/@yahoo.com\s*$/.test(myemail)) {
console.log("it ends in @yahoo");
}
is true if the string ends in @yahoo.com
(plus optional whitespace).
You do not need to use regex for this.
You can see if a string contains another string using the indexOf
method.
var idx = emailAddress.indexOf('@yahoo.com');
if (idx > -1) {
// true if the address contains yahoo.com
}
We can take advantage of slice()
to implement "ends with" like so:
var idx = emailAddress.lastIndexOf('@');
if (idx > -1 && emailAddress.slice(idx + 1) === 'yahoo.com') {
// true if the address ends with yahoo.com
}
In evergreen browsers, you can use the built in String.prototype.endsWith() like so:
if (emailAddress.endsWith('@yahoo.com')) {
// true if the address ends with yahoo.com
}
See the MDN docs for browser support.
function emailDomainCheck(email, domain)
{
var parts = email.split('@');
if (parts.length === 2) {
if (parts[1] === domain) {
return true;
}
}
return false;
}
:)
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