JavaScript - Simple Negative Value Detector

In this tutorial we will create a Simple Negative Value Detector using JavaScript. This code can detect a negative value in the text box when the user clicked th button. The code use conditional statement to track the negative value by putting less than or greater than symbol for the difference result. This is a free user-friendly kind of program, you can modify it and apply to your working application. We will be using JavaScript as a server-side scripting language because It allows greater control of your web page behavior than HTML alone. It is embedded in HTML that responsible to allow user to interact with the computer .

Getting started:

First you have to download bootstrap framework, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  6. </head>
  7. <nav class="navbar navbar-default">
  8. <div class="container-fluid">
  9. <a class="navbar-brand" href="https://sourcecodestr">Sourcecodester</a>
  10. </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class='text-primary'>JavaScript - Simple Negative Value Detector</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-3"></div>
  17. <div class="col-md-6">
  18. <div class="form-group">
  19. <label>Enter a number</label>
  20. <input type="text" id="digit" class="form-control"/>
  21. <br />
  22. <div id="result"></div>
  23. <br />
  24. <center><button class="btn btn-primary" onclick="checkNumber();">Check</button></center>
  25. </div>
  26. </div>
  27. </div>
  28. <script src="js/script.js"></script>
  29. </body>
  30. </html>

Creating the Script

This code contains the script of the application. This code will detect the negative digit when the button is clicked. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.
  1. function checkNumber(){
  2. var digit = document.getElementById('digit').value;
  3.  
  4. if(digit === ""){
  5. alert("Please complete the required field");
  6. }else{
  7. if (digit >= 0) {
  8. result = "<center><span style='font-size:20px;' class='text-primary'>"+digit + "</span> is not a Negative Number.</center>";
  9. document.getElementById('result').innerHTML = result;
  10. }else{
  11. result = "<center><span style='font-size:20px;' class='text-danger'>"+digit + "</span> is a Negative Number.</center>";
  12. document.getElementById('result').innerHTML = result;
  13. }
  14. }
  15. }
There you have it we successfully created a Simple Negative Value Detector using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment