Products
Services
Contact Us

Scripts: Javascript :: Forms :: Library Article #7

Developer's Section

A Simple Integer / Decimal Input Validator
By: Erobo Team Member

Hire a Developer for Related Work / Installation | $55 hr
Rating:  | Rate It:   
Average Votes: (1816)
Favorites:

Learn to build a simple integer / decimal validator for your forms.

In this tutorial we will learn how to validate an input field that requires only integers or decimal values. Sometimes this type of validation is necessary because it can prevent database SQL errors and / or anomalies. In the following script we show you how to install a simple Javascript input validator that can prevent users from entering nonnumeric characters or invalid numbers.

*Note: The method described below can valiate negative or positive integers/decimals numbers.

Please look at the following example:

Check for Valid Numbers:
Enter a Positive Integer:
Enter Integer or Decimal Number:
Enter a Positive / Negative Number or Decimal Number:

Let's create this simple javascript in a simple 2 Step process.

Step 1: Create the Javascript


 Code Snippet 1
<script language="Javascript">

function isNumber(s , checkFloat, checkNegative) {
     var Found = false
     var i;
     var dCheck = false
     for (i = 0; i < s.length; i++)
     {   
         // Check that current character is number.
         var c = s.charAt(i);

         if((c == "-") && (i == 0) && (s.length > 0)) {
           //check negative numbers
           if(checkNegative == false) {
             Found = true
           }
         }
         else {
           if( ((c == ".") && (checkFloat == true) && (dCheck == false)))
           {
           //pass . operator when checking decimal value
             dCheck = true
           } 
           else if (((c < "0") || (c > "9")))
           {
               Found = true
           }
       }
     }
     if( s.length == 0)
     {
         Found = true
     }

     if(Found == true)
     {
      alert("Please enter a valid number")
     }
     else
     {
        alert("this number is valid")
     }
     
 }
</script>

Step 2: Create a Simple form where you can use the javascript described above:


 Code Snippet 2

<form name="check_numbers">
<table border="1" style="border-collapse: collapse">
<tr>
     <td colspan="3">Check for Valid Numbers:</td>
</tr>
<tr>
     <td>Enter a Positive Integer:</td>
     <td><input type="text" name="myInteger"></td>
     <td><input type="button" value="validate"
      onclick="isNumber(this.form.myInteger.value, false, false)"></td>
</tr>
<tr>
     <td>Enter Integer or Decimal Number:</td>
     <td><input type="text" name="myInteger2"></td>
     <td><input type="button" value="validate"
      onclick="isNumber(this.form.myInteger2.value, true, false)"></td>
</tr>
<tr>
     <td>Enter a Positive / Negative Number or Decimal Number:</td>
     <td><input type="text" name="myInteger3"></td>
     <td><input type="button" value="validate"
      onclick="isNumber(this.form.myInteger3.value, true, true)"></td>
</tr>
</table>
</form>

The end. Good luck!.


See other Scripts in Forms

 

Submit Your Scripts:


System Message:
  • Your Javascript script has been sent. Please allow 48 hours for processing.


If you would like to have your Javascripts published in this section please fill out
the form below:
*Your Name or Username:
Home Town:
*Email:
*Description and Code:
*Enter Code shown
to the right:

[ Refresh Image ]