Home » JavaScript

JavaScript | Code to input age and check person is eligible for voting or not

JavaScript function to check whether input age is eligible for voting or not?
Submitted by Pankaj Singh, on October 12, 2018

Input age of the person and we have to write a function to validate whether person is eligible for voting or not in JavaScript?

Code 1: Input age and print message in body section

<html lang="en">
<head>
    <script>
        function validate(age){
            var ans="not eigible";
            if(age>=18){
                ans="eligible";
            }
            return(ans);
        }
    </script>
</head>
<body>
    <script>
        var age=parseInt(prompt("Enter age"));
        var status=validate(age);
        document.write("You are <b>"+status+"</b> for Vote");
    </script>
</body>
</html>

Code 2: Input age in body section and print the message through function

<head>
    <script>
        function validate(age){
            var ans="not eigible";
            if(age>=18){
                ans="eligible";
            }
            document.write("You are <b>"+ans+"</b> for Vote");
        }
    </script>
</head>
<body>
    <script>
        var age=parseInt(prompt("Enter age"));
        validate(age);
    </script>
</body>
</html>

Code 3: Input age from the function and print the message in body section

<html lang="en">
<head>
    <script>
        function validate(){
            var age=parseInt(prompt("Enter age"));
            var ans="not eigible";
            if(age>=18){
                ans="eligible";
            }
            return(ans);
        }
    </script>
</head>
<body>
    <script>
        var status=validate();
        document.write("You are <b>"+status+"</b> for Vote");
    </script>
</body>
</html>

Code 4: Input age from the function and printing the message from the function also

<html lang="en">
<head>
    <script>
        function validate(){
            var age=parseInt(prompt("Enter age"));
            var ans="not eigible";
            if(age>=18){
                ans="eligible";
            }
            document.write("You are <b>"+ans+"</b> for Vote");
        }
    </script>
</head>
<body>
    <script>
        validate();
    </script>
</body>
</html>

Output

Js code output

Js code output






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.