標籤:amp this log 小數 nbsp input pos tran 4.4
//校正輸入價格等,保留2位小數function clearNoNum(obj){ obj.onkeyup = function(event){ var e = event || window.event; if (e.keyCode == 37 || e.keyCode == 39) {//允許輸入左右方向鍵 return; } if(((event.keyCode>=48&&event.keyCode<=57)||(event.keyCode>=96&&event.keyCode<=105))) { return; } obj.value = obj.value.replace(/[^\d.]/g,""); //清除“數字”和“.”以外的字元 obj.value = obj.value.replace(/\.{2,}/g,"."); //只保留第一個. 清除多餘的 obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$","."); obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,‘$1$2.$3‘);//只能輸入兩個小數 if(obj.value.indexOf(".")< 0 && obj.value !=""){//以上已經過濾,此處控制的是如果沒有小數點,首位不能為類似於 01、02的金額 obj.value= parseFloat(obj.value); } };}//校正輸入整數不包括負整數function clearNoInteger(obj){ obj.onkeyup = function(event){ var e = event || window.event; if (e.keyCode == 37 || e.keyCode == 39) {//允許輸入左右方向鍵 return; } if(!((event.keyCode>=48&&event.keyCode<=57)||(event.keyCode>=96&&event.keyCode<=105))) { obj.value = obj.value.replace(/[^\d]/g,"");//清除“數字”和“.”以外的字元 var numarr = obj.value.split(‘‘); if (numarr[0] == 0) { var j = 0; for (var i=0;i <numarr.length; i++) { if (numarr[i] != 0) { j = i; break; } } obj.value = obj.value.substring(j,obj.value.length) } } if (((event.keyCode>=48&&event.keyCode<=57)||(event.keyCode>=96&&event.keyCode<=105))) { var numarr = obj.value.split(‘‘); if (numarr[0] == 0) { var j = 0; for (var i=0;i <numarr.length; i++) { if (numarr[i] != 0) { j = i; break; } } obj.value = obj.value.substring(j,obj.value.length) } } };}function isPositiveNum(s){//是否為正整數 var re = /^[0-9]*[1-9][0-9]*$/ ; return re.test(s) }function validate(num){//是否為正數 var reg = /^\d+(?=\.{0,1}\d+$|$)/ if(reg.test(num)) return true; return false ; }function stripscript(s) {//過濾特殊字元 var pattern = new RegExp("[`[email protected]#$^&*()=|{}‘:;‘,\\[\\].<>/?~!@#¥……&*()——|{}【】‘;:”“‘。,、?]") var rs = ""; for (var i = 0; i < s.length; i++) { rs = rs + s.substr(i, 1).replace(pattern, ‘‘); } return rs;}function containSpecial(s) { //判斷是否包含非法字元 var containSpecial = RegExp(/[(\ )(\~)(\!)(\@)(\¥)(\#)(\$)(\%)(\^)(\&)(\*)(\()(\))(\-)(\_)(\+)(\=)(\[)(\])(\{)(\})(\|)(\\)(\;)(\:)(\‘)(\")(\,)(\.)(\/)(\<)(\>)(\?)(\)]+/); return (containSpecial.test(s)); }function getByteLen(val) {//計算字元長度(中文算2個字元,英文算1個字元) var len = 0; for (var i = 0; i < val.length; i++) { var a = val.charAt(i); if (a.match(/[^\x00-\xff]/ig) != null) { len += 2; } else { len += 1; } } return len;}//產生對應隨機數function GetRandomNum(Min,Max,type){ var Range = Max - Min; var Rand = Math.random(); var num = (Min + Math.round(Rand * Range)); if(type = 2){ num = parseFloat(Math.random().toFixed(1)) + parseFloat(num); }else if(type == 3){ num = parseFloat(Math.random().toFixed(2)) + parseFloat(num); } return num; }//去除前後空格function trim(s){ return s.replace(/(^\s*)|(\s*$)/g, "");}//擷取隨機數function getRandom() { return (((1+Math.random())*0x10000)|0).toString(16).substring(1);}//產生隨機數idfunction getId() { return (getRandom()+getRandom()+"-"+getRandom()+"-"+getRandom()+"-"+getRandom()+"-"+getRandom()+getRandom()+getRandom());}//對Date的擴充,將 Date 轉化為指定格式的String//月(M)、日(d)、小時(h)、分(m)、秒(s)、季度(q) 可以用 1-2 個預留位置, //年(y)可以用 1-4 個預留位置,毫秒(S)只能用 1 個預留位置(是 1-3 位的數字) //例子: //(new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 //(new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18 Date.prototype.Format = function (fmt) { //author: meizz var o = { "M+": this.getMonth() + 1, //月份 "d+": this.getDate(), //日 "h+": this.getHours(), //小時 "m+": this.getMinutes(), //分 "s+": this.getSeconds(), //秒 "q+": Math.floor((this.getMonth() + 3) / 3), //季度 "S": this.getMilliseconds() //毫秒 }; if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); for (var k in o) if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); return fmt;}
js常見input校正