Aim: Write an HTML page that has one input, which can take multi-line text and a submit button. Once the user clicks the submit button, it should show the number of characters, words and lines in the text entered using an alert message. Words are separated with white space and lines are separated with new line character.

Program:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Example-3</title>
    <style>
        body {
            background-color: yellow;
            text-align: center;
        }
        .container {
            margin-top: 50px;
        }
        textarea {
            width: 300px;
            height: 100px;
            margin: 10px;
        }
        button {
            margin: 10px;
        }
    </style>
</head>
<body>

<div class="container">
    <h1>Program:</h1> 
    <form name="f1">
        <textarea name="tb1" placeholder="Enter text"></textarea>
        <br>
        <button onclick="return validateForm()" type="button">Submit</button>
    </form>
    <hr/>
</div>

<script>
function validateForm() {
    var d = document.forms["f1"];

    if (d.tb1.value.trim() === "") {
        alert("Please Enter Text");
        d.tb1.focus();
        return false;
    }

    var str = d.tb1.value.trim();
    var str_length = str.length;
    var str_lines = str.split(/\r\n|\r|\n/).length;
    var no_of_words = countWords(str);

    alert('Number of Words: ' + no_of_words + ' | Length: ' + str_length + ' | Number of Lines: ' + str_lines);
}

function countWords(str) {
    if (str.trim() === "") {
        return 0;
    }
    return str.trim().split(/\s+/).length;
}
</script>

</body>
</html>
        
;