How to display error without alert box using javascript ?

Errors in JavaScript can be displayed without the use of alert boxes but using the alert box is the traditional way to do that. We can show errors with two methods without using the alert box.

1. Code:

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport"
        h1 {
            color: green;
        .container {
            padding: 15px;
            width: 400px;
        input {
            margin-bottom: 10px;
        button {
            float: right;
            margin-right: 10px;
            background-color: green;
        <b>Display error without alert box</b>
        <div class="container">
                <input type="text" size="40">
                <label>Phone no:</label>
                <input type="text"
                       id="number" size="40">
            <div>   <span id="error"></span> </div>
            <button type="submit"
    function errorMessage() {
        var error = document.getElementById("error")
        if (isNaN(document.getElementById("number").value))
            // Changing content and color of content
            error.textContent = "Please enter a valid number"
   = "red"
        } else {
            error.textContent = ""

2. Syntax:

By using textContent property. The textContent is basically used to change the content of any node dynamically. With the help of this property we can display any content and draw user’s attention just like alert boxes

node.textContent = "Some error message" = "red";  


Run the above program to see the example