• We just launched and are currently in beta. Join us as we build and grow the community.

JavaScript - Simple Negative Value Detector

FatWeeabo

Prankster
F Rep
0
0
0
Rep
0
F Vouches
0
0
0
Vouches
0
Posts
131
Likes
51
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
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. <body

    >
  8. <nav class

    =

    "navbar navbar-default"

    >
  9. <div

    class

    =

    "container-fluid"

    >
  10. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://sourcecodestr"

    >

    Sourcecodester</

    a

    >
  11. </

    div

    >
  12. </

    nav>
  13. <div

    class

    =

    "col-md-3"

    ></

    div

    >
  14. <div

    class

    =

    "col-md-6 well"

    >
  15. <h3

    class

    =

    'text-primary'

    >

    JavaScript - Simple Negative Value Detector</

    h3

    >
  16. <hr

    style

    =

    "border-top:1px dotted #ccc;"

    /

    >
  17. <div

    class

    =

    "col-md-3"

    ></

    div

    >
  18. <div

    class

    =

    "col-md-6"

    >
  19. <div

    class

    =

    "form-group"

    >
  20. <label

    >

    Enter a number</

    label

    >
  21. <input

    type

    =

    "text"

    id

    =

    "digit"

    class

    =

    "form-control"

    /

    >
  22. <br

    /

    >
  23. <div

    id

    =

    "result"

    ></

    div

    >
  24. <br

    /

    >
  25. <center

    ><button

    class

    =

    "btn btn-primary"

    onclick

    =

    "checkNumber();"

    >

    Check</

    button

    ></

    center

    >
  26. </

    div

    >
  27. </

    div

    >
  28. </

    div

    >
  29. <script

    src

    =

    "js/script.js"

    ></

    script

    >
  30. </

    body

    >
  31. </

    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. if

    (

    digit ===

    ""

    )

    {
  4. alert(

    "Please complete the required field"

    )

    ;
  5. }

    else

    {
  6. if

    (

    digit >=

    0

    )

    {
  7. result =

    "<center><span style='font-size:20px;' class='text-primary'>"

    +

    digit +

    "</span> is not a Negative Number.</center>"

    ;
  8. document.getElementById

    (

    'result'

    )

    .innerHTML

    =

    result;
  9. }

    else

    {
  10. result =

    "<center><span style='font-size:20px;' class='text-danger'>"

    +

    digit +

    "</span> is a Negative Number.</center>"

    ;
  11. document.getElementById

    (

    'result'

    )

    .innerHTML

    =

    result;
  12. }
  13. }
  14. }

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!


Download
You must upgrade your account or reply in the thread to view hidden text.
 

450,695

323,213

323,222

Top