2011-05-09 121 views
2

//在這裏我只想插入numbers.how可以嗎?jQuery驗證幫助

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; 
     } 
    } 

回答

1

嘗試任何字符串,它是不是Number數字轉換將返回NaN,所以:

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

/*不工作* /功能validate_proprty_price() \t { \t \t如果(isNaN(數字(價格)) \t \t { \t \t \t proprty_price.addClass( 「錯誤」); \t \t \t proprty_price_span.text(「請輸入價格」); \t \t \t proprty_price_span.addClass(「message_error2」); \t \t \t return false; \t \t} \t \t否則{ \t \t \t proprty_price.removeClass( 「錯誤」); \t \t \t proprty_price_span.text(「」); \t \t \t proprty_price_span.removeClass(「message_error2」); \t \t \t return true; \t \t} \t} – user741996 2011-05-09 09:39:06

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" />