I need this function to return a boolean. This code seems to work well for me I’m just wondering if it’s considered a boolean?
<script type="text/javascript">
function validateEmail(email){
if (email = emailPattern){
var emailParagraph = document.createElement("p");
document.body.appendChild(emailParagraph);
emailParagraph.appendChild(document.createTextNode('Your Email is Valid'));
} else{
var emailParagraph = document.createElement("p");
document.body.appendChild(emailParagraph);
emailParagraph.appendChild(document.createTextNode('Your Email is Invalid'));
}}
var email = "jlusty@test.ca";
var emailPattern = /[\w-\.]+@([\w-]+\.)+[\w-]{2,4}/;
var matchResults = emailPattern.exec(email);
validateEmail();
</script>
[off-topic]
Hi @jlusty when you post code in the forum, you need to format it. To do so you can either select all the code and click the </> button, or type 3 backticks ``` on a separate line both before and after the code block.
Hi @jlusty, to get a boolean value use the RegExp test() method:
var isValidEmail = emailPattern.test(email)
Using exec() would generally work too as it returns an array of matches or null if there is none, which evaluates to true or false respectively; however, your code won’t quite work as expected anyway since you are assigning email to emailPattern inside the if test, which again will always evaluate to true. What you should do instead is pass the test() result like so:
function validateEmail (isValid) {
if (isValid) {
// ...
} else {
// ...
}
}
Or do the actual test inside that function (which would be more appropriate IMO, given the name of that function):
var emailPattern = /[\w-\.]+@([\w-]+\.)+[\w-]{2,4}/
function validateEmail (email) {
if (emailPattern.test(email)) {
// ...
} else {
// ...
}
}
BTW you can simplify your code quite a bit as the creation of the paragraph element could be done outside the if block – you might actually use a ternary expression instead here:
var emailPattern = /[\w-\.]+@([\w-]+\.)+[\w-]{2,4}/
function validateEmail (email) {
var emailParagraph = document.createElement('p')
emailParagraph.textContent = emailPattern.test(email)
? 'Your email is valid'
: 'Your email is not valid'
document.body.appendChild(emailParagraph)
}
validateEmail('jlusty@test.ca')
Returning a boolean, basically means returning ‘true’ or ‘false’
emailPattern.test(email) // returns true if it matches or false if it doesn't
or
function largerThanTen ( num ) {
return ( num > 10 ) // returns a boolean true or false
}
largerThanTen(15) // true
Trying to explain how we get to ‘true’ or ‘false’ is a bit complicated. Javascript will convert/coerce data 0, "", true, 1, [] etc. in to booleans equivalents
For instance
let str = "";
if ( str ) { // empty string is converted to a boolean of false
//... we won't get here then
} else {
console.log('string is empty') // we end up here
}
I am trying to find you some good basic explanations on the subject, and it’s not easy to find.
First let clarify something about Booleans. A Boolean is a data type that has two possible values: TRUE or FALSE. Anything else is NOT a boolean. Sometimes it’s trick because you can evaluate some non-Boolean values as FALSE (example: ‘’, 0, NULL, UNDEFINED) and other evaluate as TRUE( ‘a string’, 1, etc.). But even tho, for example, ‘a string’ evaluates as TRUE it is not a Boolean …it is a string.
Right now you have a couple of bugs in your
if (email = emailPattern){...} should be if (matchResults){..} otherwise your code is setting the variable email to the value stored in emailPattern, calling it passed, and execution the true code block. Essentially every email would validate. Not what I think you want.
Your function is not returning anything at all ( which always evaluates to FALSE, but is also…NOT a Boolean). You have no return statements. If NEED to return a BOOLEAN VALUE ( as opposed to something ‘truth’ or ‘falsey’ , you need something like:
{
var emailParagraph = document.createElement("p");
document.body.appendChild(emailParagraph);
emailParagraph.appendChild(document.createTextNode('Your Email is Valid'));
return true;
} else{
var emailParagraph = document.createElement("p");
document.body.appendChild(emailParagraph);
emailParagraph.appendChild(document.createTextNode('Your Email is Invalid'));
return false;
}
Here is a more efficient way to solve this:
function validateEmail(email, emailPattern){
//uses your email patter as a defaul, but allows the fuction to take RegExp al well
emailPattern = (emailPattern instanceof RegExp) ? emailPattern : /[\w-\.]+@([\w-]+\.)+[\w-]{2,4}/;
// checks email against email pattern; the RegExp.test method always returns a Boolean value
var matchResults = emailPattern.test(email);
// selects a message to output
var message = matchResults ? 'Your Email is Valid' : 'Your Email is Invalid' ;
//outputs message
var emailParagraph = document.createElement("p");
document.body.appendChild(emailParagraph);
emailParagraph.appendChild(document.createTextNode(message));
// returns the Boolean;
return matchResults;
}
validateEmail('jlusty@test.ca');