我需要一个货币掩码作为输入,问题是我无法在我的应用程序上使用 jquery。
例如,如何使用正则表达式“on key up”来执行此操作?
这是我的代码:
<tr>
<td>
money:
<input type="text" class="money" maxlength="22" id="money" name="money">
</td>
</tr>
我的格式看起来像00.000,00
最佳答案
由于您没有提供格式,因此无法给您直接答案...
在字符串上测试正则表达式的 JavaScript 方法是:
function regexmoney(){
var patt=new RegExp(pattern,modifiers);
var string = document.getElementById('money').value;
var result = patt.test(string);//Returns true/false
if(!result){
var charArray=string.split();
//Iterate through the array and arrange the chars as
//you see fit (returning moneyresult)
document.getElementById('money').value = moneyresult;
}
}
然后是 HTML:
<tr>
<td>
money:
<input type="text" onkeyup="regexmoney()" class="money" maxlength="22" id="money" name="money">
</td>
</tr>
并生成正则表达式 http://txt2re.com/
希望这有帮助...
关于JavaScript 正则表达式 for Money 非 JQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17192946/