2017-08-01 86 views
0

嗨嗬做我格式化這一個數字字段,你只能使用帶有逗號和小數點(這是一個價格字段)前150,850.00價格數字格式

而且分離數如何讓出現的逗號和小數點自動生成?

<input 
    placeholder="<?php osc_esc_html(_e('Price', 'ctg_housing')) ; ?> 
    <?php osc_esc_html(_e('Min', 'ctg_housing')) ; ?>" 
    type="text" id="priceMin" name="sPriceMin" 
    value="<?php echo osc_esc_html(osc_search_price_min()); ?>" 
    onkeypress="return isNumber(event)" /> 

感謝

回答

0

也許是這樣的:

$(document).ready(function(){ 
    $('#priceMin').keyup(function(event){ 
     var $elm = $(this); 
     var value = $elm.val(); 
     var result = value.replace(/[^0-9\.\,]+/g, ""); 
     $elm.val(result); 
    }); 
}); 
+0

這不工作 – Rhoda

+0

我已經testet它,所以它應該 - 你的jQuery包括? –

0

下面是JS代碼,將添加comma的數字,並會從數字中刪除逗號一個代碼。這是一個接受數的函數,這樣你就可以在任何地方使用它:

function getNumberWithCommas(yourNumber) { 
 
    var n = yourNumber.toString().split("."); 
 
    n[0] = n[0].replace(/\B(?=(\d{3})+(?!\d))/g, ","); 
 
    return n.join("."); 
 
} 
 
var numberWithComma = getNumberWithCommas(1136324); 
 
console.log(numberWithComma); 
 

 
numberWithComma = numberWithComma.replace(/\,/g, ''); 
 
console.log(parseInt(numberWithComma));