/    Sign up×
Community /Pin to ProfileBookmark

JS condition in HTML

I have the following HTML code:
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>
<html xmlns=”http://www.w3.org/1999/xhtml“>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<title>Untitled Document</title>
</head>

<body>
<form id=”form1″ name=”form1″ method=”post” action=””>
<label>Age
<input type=”text” name=”Age” />
</label>
</form>
</body>
</html>

<html>
<body>

</html>

</body>
</html>
If the user enter an Age value < 18 then a popup must display “You too young”
If the user enter an Age value =>18 a html page must load.
Please help me how to write this code
Thanking you
Marius

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERAug 08.2020 — You have two HTML-BODY regions on the same page. Second won't load or display.

You have invalid HTML and no javascript or CSS shown. What have you tried?

We will be glad to help but not do homework assignments. :)
Copy linkTweet thisAlerts:
@NaheedAug 09.2020 — @marius123#1621905 Thanks for asking,

You did not define the javascript and CSS on the page for the condition and just taking input no processing code defined. Also you there are two tags for HTML and BODY will not read by the browser.
Copy linkTweet thisAlerts:
@VITSUSAAug 10.2020 — @marius123#1621905 I agree with JMRKER and Naheed, you need to define the javascript and CSS on the page for the condition.
Copy linkTweet thisAlerts:
@marius123authorAug 13.2020 — Thank you for the guidance

The following code works for me:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<!DOCTYPE html>

<body>

<!DOCTYPE html>

<form action="" onsubmit="myFunction()">

<label for="fname">First name:</label><br>

<input type="text" id="fname" name="fname" value=""><br>

<label for="lname">Last name:</label><br>

<input type="text" id="lname" name="lname" value=""><br><br>

<input type="submit" value="Submit">

</form>

<script type="text/javascript">

function myFunction(){

var fname = document.getElementById("fname").value;

if(fname > 39)

alert("Condition met")

else

alert("Condition NOT met")

}

</script>

But now I have 2 questions:
  • 1. If I wish to display the reply "Condition met" under the form and not on a pop up caused by Alert - how do I do that

  • 2. In stead of returning when the condition is met "condition met" to load a webpage?

  • 3. Help would be appreciated.

  • 4. Thanks
  • Copy linkTweet thisAlerts:
    @JMRKERAug 14.2020 — Difficult to see how it verifies when you have 3 different <doctype ...> statements

    and no ending </body> or </html> tags.

    Why are you using definitions from 1999 when you have a 2020 browser?

    The "type='text/javascript'" is no longer needed in modern browsers.

    Why are you testing the first name to be greater than 39? Is this for a prison inmate?

    What are you doing with the 'onsubmit' condition when you don't return anything from your function?

    What is the webpage to load if you do get success? I don't see it defined anywhere.

    You may want to go back and re-read that section of this homework assignment (???).

    ×

    Success!

    Help @marius123 spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.25,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...