5

我已经编写(并复制)了几行 Javascript,它很好地满足了我的目的。但我试图找出一种更好的方法(跨浏览器和更好的性能)来做到这一点。我从朋友那里复制了该isInteger函数,但我不明白为什么我们在以下情况下检查字符串值:

if (((c < "0") || (c > "9"))) return false;

上述条件工作正常,但是当我将其更改为检查数值时,功能会中断。输入字段开始接受字母字符。这是我更改它时的样子:

if ((( c < 0 ) || ( c > 9 ) return false;

我试图注释掉部分,以便您了解正在发生的事情。此代码中是否还有任何安全漏洞?我读到 1innerHTML1 方法可以打开一些安全漏洞,因此我们需要对其执行“干净”操作。因此我选择使用 jQuery 的.html方法(我是 JavaScript 新手)

有问题的页面:http ://thehotdeal.net/clients/wehtmlit/index.php?order/

$(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
  */
  /* <![CDATA[ */
    var total_price_main_pages = ($("#pages").attr("value")) * (<?php echo $main_price; ?>),
    total_price_sub_pages = ($("#subpages").attr("value")) * (<?php echo $sub_price; ?>);
    /*  ]] > */
    $("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: <strong>" + total + "</strong>");
  }
  // 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 <= s <= 9 ]
  function isInteger(s) {
    var i;
    for (i = 0; i < s.length; i++) {
      var c = s.charAt(i);
      if (((c < "0") || (c > "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 <= 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();
    }
  });
});
4

2 回答 2

15

我不明白为什么我们在以下条件下检查字符串值

因为c是一个字符(实际上是一个 1 字符的字符串),因为这就是String.charAt返回的内容。也就是说,isInteger可以使用正则表达式更简单地编写函数:

function isPositiveInteger(s)
{
    return !!s.match(/^[0-9]+$/);
    // or Rob W suggests
    return /^\d+$/.test(s);
}

或者您可以采取另一种方法:将字符串转换为数字,确保它是正数,并确保数字的下限与原始数字相同(因此它是整数):

function isPositiveInteger(s)
{
    var i = +s; // convert to a number
    if (i < 0) return false; // make sure it's positive
    if (i != ~~i) return false; // make sure there's no decimal part
    return true;
}
于 2012-11-04T15:29:47.337 回答
0

关于什么..

if( +inputString > 0 ) {
}

如果它只是要找出输入是否为正整数。如果您也不想允许带有小数点/浮点值的数字,您应该使用输入字段验证,例如

<input type="text" pattern="\d+" required/>

这告诉输入字段只有numbers。该标志是可选的,如果存在,除非满足所有模式required,否则它将不允许任何提交按钮继续。

于 2012-11-04T15:32:20.887 回答