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.
Method 1: 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
- Syntax:
node.textContent = "Some error message" // To draw attention node.style.color = "red";
- Example:
<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>Demo</title><style>h1 {color: green;}.container {padding: 15px;width: 400px;}label,input {margin-bottom: 10px;}button {float: right;margin-right: 10px;background-color: green;}</style></head><body><center><h1>GeeksforGeeks</h1><b>Display error without alert box</b><br><br><divclass="container"><div><label>Uername:</label><inputtype="text"size="40"></div><div><label>Phone no:</label><inputtype="text"id="number"size="40"><spanid="error"></span></div><buttontype="submit"onclick="errorMessage()">Submit</button></div></center></body><script>function errorMessage() {var error = document.getElementById("error")if (isNaN(document.getElementById("number").value)){// Changing content and color of contenterror.textContent = "Please enter a valid number"error.style.color = "red"} else {error.textContent = ""}}</script></html>chevron_rightfilter_none - Output:

Method 2: By using innerHTML property. The innerHTML can be used to changed selected node’s HTML. Instead of textContent and innerHTML, one can also use innerText which will lead to same output.
- Syntax:
node.innerHTML = "<span style='color: red;'> Please enter a valid number </span>" - Example:
<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>Demo</title><style>h1 {color: green;}.container {padding: 15px;width: 400px;}label,input {margin-bottom: 10px;}button {float: right;margin-right: 10px;background-color: green;}</style></head><body><center><h1>GeeksforGeeks</h1><b>Display error without alert box</b><br><br><divclass="container"><div><label>Uername:</label><inputtype="text"size="40"></div><div><label>Phone no:</label><inputtype="text"id="number"size="40"><spanid="error"></span></div><buttontype="submit"onclick="errorMessage()">Submit</button></div></center></body><script>function errorMessage() {var error = document.getElementById("error")if (isNaN(document.getElementById("number").value)){// Changing HTML to draw attentionerror.innerHTML = "<spanstyle='color: red;'>"+"Please enter a valid number</span>"} else {error.innerHTML = ""}}</script></html>chevron_rightfilter_none - Output:

Recommended Posts:
- How to Display Changed Browser URL Without Reloading Through alert using JavaScript ?
- Difference between alert box and confirmation box in JavaScript
- How to get array structure with alert() in JavaScript?
- JavaScript | Error name Property
- JavaScript | Error message Property
- JavaScript Error Handling: Unexpected Token
- JavaScript | Error.prototype.toString() Method
- How to solve “Submit is not a function” error in JavaScript ?
- Javascript | Error and Exceptional Handling With Examples
- How do you display JavaScript datetime in 12 hour AM/PM format?
- Display the number of links present in a document using JavaScript
- How to display hidden element when a user starts typing using JavaScript ?
- How to display confirmation dialog when clicking an <a> link using JavaScript / jQuery ?
- How to pop an alert message box using PHP ?
- How to add a new line in the alert box ?
- How to change the style of alert box using CSS ?
- HTML | Window alert( ) Method
- What is the difference between display: inline and display: inline-block in CSS?
- How to Remove extra right margin when using alert-dismissible ?
- How to load notification alert on top right corner without click of button in bootstrap ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


