title: Check for positive integers in Javascript: performance and security
body: <p>I have written (and copied) a few lines of Javascript and it serves my purpose well. But I am trying to figure out a better way (cross-browser and better performance) of doing this. I copied the <code>isInteger</code> function from a friend but I do not understand why we are checking against a string value in the following condition:</p>
  
  <pre><code>if (((c &lt; "0") || (c &gt; "9"))) return false;
  </code></pre>
  
  <p>The above condition works fine but when I change it to check against number values, the functionality breaks. The input field starts accepting alphabetical characters. Here is how it looks when I change it:</p>
  
  <pre><code>if ((( c &lt; 0 ) || ( c &gt; 9 ) return false;
  </code></pre>
  
  <p>I have tried to comment out sections so that you can understand what's happening. Also are there any security holes in this code? I read that the 1innerHTML1 method can open some security holes and hence we need to perform a 'clean' operation with it. Hence I chose to use jQuery's <code>.html</code> method (I am new to JavaScript)</p>
  
  <p>The page in question: <a href="http://thehotdeal.net/clients/wehtmlit/index.php?order/" rel="noreferrer">http://thehotdeal.net/clients/wehtmlit/index.php?order/</a></p>
  
  <pre><code>$(document).ready(function() {
    var total = 0;
    function calcTotal() {
    /* fetching some values from PHP variables and then performing calculations.
      essentially this is multiplying number of pages by price per page
    */
    /* &lt;![CDATA[ */
      var total_price_main_pages = ($("#pages").attr("value")) * (&lt;?php echo $main_price; ?&gt;),
      total_price_sub_pages = ($("#subpages").attr("value")) * (&lt;?php echo $sub_price; ?&gt;);
      /*  ]] &gt; */
      $("input.calculate:checked").each(function() {
      // This happens for each checked input field
      // These are few additional otions available to the user. If selected then
      // the price stored in their "data" attribute is added to the total
        var value = $(this).attr("data");
        total += parseInt(value); 
      });
      total += (parseInt(total_price_main_pages)) + (parseInt(total_price_sub_pages));
      $("#total").html("Total: &lt;strong&gt;" + total + "&lt;/strong&gt;");
    }
    // This happens when the page loads
    calcTotal();
    $("input.calculate").click(function() {
      total = 0;
      calcTotal();
    });
    // function to check if an input is positive number(s). returns true if [ 0 &lt;= s &lt;= 9 ]
    function isInteger(s) {
      var i;
      for (i = 0; i &lt; s.length; i++) {
        var c = s.charAt(i);
        if (((c &lt; "0") || (c &gt; "9"))) return false;
      }
      return true;
    }
    // Checking the mainpage input (default value 1)
    // (valid value is greater than or equal to 1 and less than 10)
    $("#pages").keyup(function() {
      var page = $(this).val();
      // if user deletes the value in this input (blank)
      // then just display a warning message and do nothing
      if(page == ""){
        this.value = "";
        $("#pageError").html("Please enter a value equal or greater than 1.");
        return false;
      }
      // if value is less than or equal to zero then
      // then set 1 as the new value, remove the error message and call the calcTotal function
      else if(page &lt;= 0){
        this.value =1;
        $("#pageError").empty();
        total = 0;
        calcTotal();
      }
      // check if value is not a positive integer by calling the isInteger function
      // if not a positive integer then set 1 as the new value,
      //remove the error message and call the calcTotal function
      else if(!isInteger(page)){
        this.value =1;
        $("#pageError").empty();
        total = 0;
        calcTotal();
      }
      // if value does not fall in any of the if statements i.e. value is acceptable
      // remove the error message and call the calcTotal function
      $("#pageError").empty();
      total = 0;
      calcTotal();
    });
    // check if value is not empty when user exits the input
    // if empty then set value as 1, remove error message and call calcTotal function
    $("#pages").blur(function() {
      var page = $(this).val();
      if(page == ""){
        this.value = 1;
        $("#pageError").empty();
        total = 0;
        calcTotal();
      }
    });
    // Checking the subpage input (default value 0)
    // (valid value is greater than or equal to 0 but less than 10)
    $("#subpages").keyup(function() {
      var page = $(this).val();
      if(page == ""){
        this.value = "";
        return false;
      } else if(!isInteger(page)){
        this.value = 0;
        total = 0;
        calcTotal();
      }
      total = 0;
      calcTotal();
    });
    $("#subpages").blur(function() {
      var page = $(this).val();
      if(page == ""){
        this.value = 0;
        total = 0;
        calcTotal();
      }
    });
  });
  </code></pre>