Home  >  Article  >  Web Front-end  >  js function code to limit text box to integer and currency_form special effects

js function code to limit text box to integer and currency_form special effects

WBOY
WBOYOriginal
2016-05-16 18:18:43847browse

Limited to integers

<html>
<head>
<script language="javascript" type="text/javascript">
 var G=document.getElementById;
 function checkNumber(){
  var reg = /^(&#63;:0|[1-9][0-9]&#63;|100)$/;
   
  var strNumber = G("txtNumber").value;
  alert(reg.test(strNumber))
 }
</script>
</head>
<body >
<input type="text" value="" id="txtNumber" title="Please enter a number witch between 0 and 100 ..">
<button onclick="checkNumber();">CheckNumber</button>
</body>
</html>

Limited to currency

<script language="JavaScript" type="text/javascript">
<!--
function checkMoney(obj){
var tempValue=obj.value.replace(/(^\s+)|(\s+$)/g,'');
if(!tempValue){return;}
if(/^-&#63;\d+(\.\d+)&#63;$/.test(tempValue)){
obj.value=parseFloat(tempValue).toFixed(2);
}else{
alert('请输入合法的货币值!');
obj.select();
return;
}
}
//-->
</script>
onblur="checkMoney(this)"
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn