Positive Negative Numbers Checker

I have here a very simple program that I create and it's called Positive Negative Numbers Checker using JavaScript jQuery. The user will be asked to input any number that they desired to check it then our program will check and determine that the given number from the user is a positive or negative number using jQuery checker script. Let's create first the markup for this simple program. All we need one TextBox where the users can type their desired number to check, two buttons, one for the check and one for the cancel. Lastly, construct another TextBox to view the results.
  1. <div style="width:35%;">
  2.    <form>
  3.       <div>
  4.          <label>Type the Number</label>
  5.          <input type="text" id="numeric_value" placeholder="Value . . . . ." autofocus="autofocus">
  6.       </div>
  7.       <div>
  8.          <label>Result</label>
  9.          <input type="text" id="number_result" style="cursor:no-drop; text-align:center; color:blue; font-size:18px;" class="form-control" readonly />
  10.       </div>
  11.       <button type="button" id="check_the_Number">Check</button>
  12.       <button type="button" id="clear">Cancel</button>
  13.    </form>
  14. </div>
For the script, this we are going to use to check the number if it is a positive or a negative number is given from the user. Check the script below.
  1. if (num >= 0) {
  2.     remarks = num + " is a Positive Number.";
  3.     $("#number_result").val(remarks);
  4. } else {
  5.     remarks = num + " is a Negative Number.";
  6.     $("#number_result").val(remarks);
  7. }
Complete Source Code
  1. <script>
  2.     $(document).ready(function() {
  3.         $("#check_the_Number").click(function() {
  4.             var num = $("#numeric_value").val();
  5.             if (jQuery.isNumeric(num) == false) {
  6.                 alert('Please enter numeric value');
  7.                 $('#numeric_value').val('');
  8.                 $("#number_result").val('');
  9.                 $('#numeric_value').focus();
  10.             } else if (num >= 0) {
  11.                 remarks = num + " is a Positive Number.";
  12.                 $("#number_result").val(remarks);
  13.             } else {
  14.                 remarks = num + " is a Negative Number.";
  15.                 $("#number_result").val(remarks);
  16.             }
  17.         });
  18.         $("#clear").click(function() {
  19.             $("#numeric_value").val('');
  20.             $("#number_result").val('');
  21.             $("#numeric_value").focus();
  22.         });
  23.     });
  24. </script>

Sample Output

The number is Positive Result The number is Negative Result

Add new comment