Checking to see if a checkbox that indicates user wants email is checked; if so, validate their email address

I’ve got the following so far. The alert never fires. I have the correct element id. Feeling really frustrated because I know it’s probably one of my newbie mistakes. Help?

if (document.getElementById("contactemail").checked = true) {

        alert("Checked!");

        /* Checks the email field to make sure that it's an email address in formHELP */

        var emailaddy2 = /^(([^<>()\[\]\\.,;:\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,}))$/;

        if (document.forms["formHELP"]["FromAddress"].value.match(emailaddy2)) {
            return true;
        }

        else {
            alert("Please input a valid email address.");
            return false;
        }
}

Is the html code loaded/ready before the JS code runs? If the DOM is not ready/loaded, JS can’t find the element,

Can we see your full code? Maybe you can make bin/fiddle on jsbin, jsfiddle or codepen?

Given return is used, you also must be using a function.

As @stetim94 has said, the whole picture of what you’re working on would be helpful.

That being said, would it not make sense to have the validation routine work only if the user has either entered something into the box (e.g. fire an event if the element loses focus) or when they check the box?

Thank you stetim, thepitycoder.

I’ve never tried a fiddle or whatever, but I found a site called JSFiddle that seems to do what you’re asking. My thing is here:

https://jsfiddle.net/dLwq1jea/

Please let me know if it’s comprehensible or if I need to upload something else.

1 Like

Sorry, forgot to reply to this bit. It might make more sense, but this particular class assignment is to see if I can do it with this sort of approach. Usually I’d hesitate to ask for help with something for a class here at Codecademy, but I learned about JavaScript (and HTML and CSS) from the training here and I’m not overfond of Stack Overflow’s layout and sometimes people are fairly unkind there, (though I understand that if I ever decide to pursue this that SO will probably be my first stop for this sort of thing).

My first question about my assignment was why not just do it with HTML5 validation, especially because people can turn JavaScript off entirely. I suppose that layering validation including server-side which we haven’t really gotten to might give one a bit more confidence though.

the JS code you posted in JSfiddle, was originally in mhpscripts.js?

So, a html file is load from top to bottom, so the JS code is loaded before the body of the html document, so the elements which JS needs to find with getElementById can’t be found.

Other things of the JS code do work?

What editor do you use? I use phpstorm, which tells me:

Unreachable code
Inspection info: Reports code which can never be executed. Such code almost certainly represents a programming error

on this line (116, give or take):

if (document.forms["formHELP"]["lastname"].value.match(letters)) {

look at the code before:

    if (document.forms["formHELP"]["firstname"].value.match(letters)) {
        return true;
    }

    else {
        alert("Please input alphabet letters only, (for your First Name.)");
        return false;
    }

Do you see a problem?

what do you mean by this sort of approach?

I’ll take a look at what’s loading when and will try a workaround, thanks stetim. :slight_smile:

1 Like

I asked so many more questions, please answer them. I don’t want you running of in the wrong direction.

Sure.

I’m using Visual Studio Community. Everything else in the JS seems to work properly. When I said “this sort of approach”, I meant validating through JavaScript rather than through HTML or something else.

1 Like

so the problem is then the unreachable code due to the return statements

1 Like



I knew that.

I mean, I knew that from the exercises here on this site, just my peabrain didn’t think of it when I was actually forming the JavaScript.

Sigh. I must be hourly. :slight_smile:

Thank you again so much.

– Mark