function checkcnnum(str) { var res=1; const zh = [‘零‘, ‘一‘, ‘二‘, ‘三‘, ‘四‘, ‘五‘, ‘六‘, ‘七‘, ‘八‘, ‘九‘,‘十‘,‘百‘,‘千‘,‘万‘]; for (let i = str.length - 1; i >= 0; i--) { if(!in_array(str[i],zh)) { res=0; } } return res; } function in_array(search,array){ for(var i in array){ if(array[i]==search){ return true; } } return false; } var str=‘一百二十一‘; var aa=checkcnnum(str); console.log(aa);
封装成一个
function checkcnnum(str) { var res=1; const zh = [‘零‘, ‘一‘, ‘二‘, ‘三‘, ‘四‘, ‘五‘, ‘六‘, ‘七‘, ‘八‘, ‘九‘,‘十‘,‘百‘,‘千‘,‘万‘,‘亿‘]; for (let i = str.length - 1; i >= 0; i--) { //循环对比 var ares=0; for(var j in zh){ if(zh[j]==str[i]){ ares=1; } } //根据对比结果判断 if(ares==0) { res=0; return res; } } return res; } var str=‘1十一‘; var aa=checkcnnum(str); console.log(aa);
原文:https://www.cnblogs.com/baker95935/p/11338812.html