1

So, I have a webpage where the user needs to input their age, if they're older than 18 years old, the screen will display "Welcome to the page!" otherwise, it will display "You're not old enough". I have all done, but I made another HTML page where the header says "Welcome to the page!". I want to link that HTML new file, and I want to load it after the user inputs their age, and if they're older than 18, I want that HTML file to come up. I have the following code:

function yourAge() {

    var age = prompt("Please enter your age:");

    if (!age) {
        alert("Please fill in all the required field!");
        return false;
    }

    var regex=/^[0-9]+$/;
    if (!age.match(regex))
    {
        alert("Must input numbers!");
        return false;
    }


    if (age >= 18) {
        document.write("Welcome to the website!");
    }

    else {
        document.write("You're not old enough to enter this website!")
    }

} 

I think I would remove the "document.write" but I'm not sure what to do in there...

If I didn't make myself clear, I basically want to redirect the user to that HTML file once their input their age, and only display it if they're older than 18, otherwise, I will create another one that says "You're not old enough".

4
  • 1
    you could use location.href = '/urlpath'; to redirect to your other page Commented Nov 24, 2016 at 21:01
  • You want to redirect? Change location.href Commented Nov 24, 2016 at 21:01
  • It worked! I didn't know about this one. I'm learning JavaScript and this totally will help me. Thanks both of you! Commented Nov 24, 2016 at 21:04
  • 1
    On a different note. Validations are best done server-side and not client-side. Commented Nov 24, 2016 at 21:05

1 Answer 1

1

You could use document.location to redirect after checks:

if (age >= 18) {
    document.write("Welcome to the website!");
    document.location = "YOUR_PAGE_URL.html";
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.