function numCheck(obj){
//如果输入非数字,则替换为'',如果输入数字,
if($(obj).val() != '') {
$(obj).val($(obj).val().replace(/[^\d]/g, ''));
}
}
html
<input maxlength='11' onkeyup='numCheck(this)' autocomplete='off' type='text' id='orgvalue'
placeholder='默认值'>
正则数字转化为大写
function smalltoBIG(n){
var fraction = ['角', '分'];
var digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
var unit = [ ['元', '万', '亿'], ['', '拾', '佰', '仟'] ];
var head = n < 0? '欠': '';
n = Math.abs(n); var s = ''; for (var i = 0; i < fraction.length; i++)
{
s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '');
}
s = s || '整';
n = Math.floor(n); for (var i = 0; i < unit[0].length && n > 0; i++)
{
var p = '';
for (var j = 0; j < unit[1].length && n > 0; j++)
{
p = digit[n % 10] + unit[1][j] + p;
n = Math.floor(n / 10);
}
s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s;
}
return head + s.replace(/(零.)*零元/, '元').replace(/(零.)+/g, '零').replace(/^整$/, '零元整');
}
校验小数点后留取几位小数
$(".moneyText").keyup(function(){
var reg = /^\d+\.?(\d{1,2})?$/;
while (!reg.test($(this).val()) && $(this).val() != "") {
$(this).val(checkStr($(this).val()));
}
});
//检查是否符合金额格式:只能输入数字且最多保留小数点后两位
function checkStr(str) {
str = str.substring(0,str.length-1);
return str;
}
限制textarea最多输入长度显示
</body>
<textarea onkeyup="checkLen(this)"></textarea>
<div>您还可以输入 <span id="count">300</span> 个文字</div>
<script type="text/javascript">
function checkLen(obj)
{
var maxChars = 300;//最多字符数
if (obj.value.length > maxChars)
obj.value = obj.value.substring(0,maxChars);
var curr = maxChars - obj.value.length;
document.getElementById("count").innerHTML = curr.toString();
}
</script>
正则校验如果输入回车或者空格替换成<br>
var Explain = $('#charDetail-box').text().replace(/[\r\n]/g,'<br>');
$('#charDetail-box').html(Explain);
网友评论