【问题标题】:jQuery validation helpjQuery 验证帮助
【发布时间】:2011-08-21 11:56:53
【问题描述】:

//在这里我只想插入数字。我该怎么做?

function validate_proprty_price()
    {
        if($("#proprty_price").val() == '')
        {
            proprty_price.addClass("error");
            proprty_price_span.text("Please Enter Price");
            proprty_price_span.addClass("message_error2");
            return false;
        }
        else{
            proprty_price.removeClass("error");
            proprty_price_span.text("");
            proprty_price_span.removeClass("message_error2");
            return true;
        }
    }

【问题讨论】:

    标签: jquery jquery-ui jquery-selectors jquery-validate


    【解决方案1】:

    jQuery Validation 脚本附加到您的代码中,例如:

    <script 
        type="text/javascript"
        src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.8/jquery.validate.min.js">
    </script>
    

    只需将numberrequired 作为一个类添加到您的输入中

    <input id="price" class="required number" />
    

    【讨论】:

      【解决方案2】:

      尝试用Number 转换任何不是数字的字符串将返回NaN,所以:

      var price = $("#proprty_price").val();
      if (isNaN(Number(price)) {
          // not a number, error message
      }
      

      【讨论】:

      • /*不工作 */ function validate_proprty_price() { if (isNaN(Number(price)) { proprty_price.addClass("error"); proprty_price_span.text("请输入价格"); proprty_price_span .addClass("message_error2"); return false; } else{ proprty_price.removeClass("error"); proprty_price_span.text(""); proprty_price_span.removeClass("message_error2"); return true; } }
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多