作者Kej (comfortably numb)
看板Ajax
標題Re: [問題] 如何動態建立檢核用的正規表示式?
時間Sat Mar 10 00:54:20 2007
※ 引述《PTTFHK ()》之銘言:
: function doCheck(num,int,dec){
: var pattern = /^\d{1,int}$|\d{1,int}\.\d{1,dec}$/;
: if(num.match(pattern)==null){
: alert("不符合規定!");
: }else{
: alert("通過檢核!");
: }
: }
可以試試改成以下這樣:
function doCheck(num,int,dec){
var p = new RegExp("^\\d{1,"+int+"}$|^\\d{1,"+int+"}\\.\\d{1,"+dec+"}$");
if(p.exec(num)==null){
alert("不符合規定!");
}else{
alert("通過檢核!");
}
}
--
...the people who are crazy enough to think
they can change the world, are the ones who do...
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.133.102.122
1F:推 PTTFHK:酷~ 非常感謝你~^^*(但後來發現需將int改成其他非保留字) 03/10 09:51