2011-02-04 65 views

回答

1

您可以通過Masked Input Plugin解決輸入價格問題。有更多的jQuery website.

驗證數據可以在格式和內容上完成。完成格式化後,您可以使用jQuery validate plugin。對於格式,我會爲地址使用web服務,但對於州和國家,您可以提供下拉式(輸入選擇)和強制選擇。

郵編例如,每個國家不同,所以你必須做出依賴於該國的選擇;)

1

代碼小數點位置:

<input type="text" class="price" value="" /> 

$('.price').keydown(function() { 
    var decPos = $(this).val().split('.'); 
    if(decPos.length > 1) 
    { 
     decPos = decPos[1]; 
     if(decPos.length >= 2) return false; 
    } 
}); 

驗證地址信息可能會在這裏找到 - http://docs.jquery.com/Plugins/validation

+0

這並不確定隨着OP要求輸入小數。 – 2011-02-04 08:34:15

1

我認爲最好的選擇是使用正則表達式。 More info

十進制例子:

var decimal = /\.\d\d$/; 
2

這裏是主要類型的驗證。

$(document).ready(function(){ 
//for numeric integer only 
var num_int_Exp = /^[0-9]+$/; 
$("body").on("keypress", ".custom_numeric_int", function(e){ 
     var keynum; 
      if(window.event){ // IE 
       keynum = e.keyCode; 
      }else 
      if(e.which){ // Netscape/Firefox/Opera     
        keynum = e.which; 
      } 
      if(!String.fromCharCode(keynum).match(num_int_Exp)) 
      { 
       return false; 
      } 
      return true; 
}); 

//for numeric float only 
$("body").on("keypress", ".custom_numeric_float", function(e){ 
    //$('.custom_numeric_float').keypress(function(event) 
     //alert($(this).val().indexOf(".")); 
     if ($(this).val().indexOf(".") > -1 && event.which == 46) { 
      return false; 
     } 

     if ((event.which != 46) && (event.which < 48 || event.which > 57)) { 
      event.preventDefault(); 
     } 
}); 


//for character input 
var charExp = /^[a-zA-Z]+$/; 
$("body").on("keypress", ".custom_char", function(e){ 
      var keynum; 
      if(window.event){ // IE 
       keynum = e.keyCode; 
      }else 
      if(e.which){ // Netscape/Firefox/Opera     
        keynum = e.which; 
      } 
      if(!String.fromCharCode(keynum).match(charExp)) 
      { 
       return false; 
      } 
      return true; 
}); 

//for alpha-numeric input 
var alphaExp = /^[a-zA-Z0-9]+$/; 
$("body").on("keypress", ".custom_alphanumeric", function(e){ 
      var keynum; 
      if(window.event){ // IE 
       keynum = e.keyCode; 
      }else 
      if(e.which){ // Netscape/Firefox/Opera     
        keynum = e.which; 
      } 
      if(!String.fromCharCode(keynum).match(alphaExp)) 
      { 
       return false; 
      } 
      return true; 
});}); 

現在給你的文本框添加適當的類,然後完成驗證。 希望這會有所幫助。