What is the error of this regular expression. I already tested it with another function and the mask works. But this one for numbers with cents, does not work. What is the error?
<html>
<body>
<script>
function mascara(o, f) {
obj=o;
fun=f;
setTimeout(execMascara(), 1);
}
function execMascara() {
obj.value=fun(obj.value);
}
function soNumeros(v) {
var v = this;
if(v.indexOf('.')==-1) {
v = v.replace(/([\d]+)/, "$1,00");
}
v = v.replace(/([\d]+)\.([\d]{1})/g, "$1,$20");
v = v.replace(/([\d]+)\.([\d]{2})/g, "$1,$2");
return v ? "R$ " + v : 'Grátis';
}
</script>
<form>
<label="numero">
Só número: <input id="numero" onkeypress="mascara(this,soNumeros)"/>
</label>
</form>
</body>
</html>
<html>
<body>
<script>
function mascara(o, f) {
obj=o;
fun=f;
setTimeout(execMascara(), 1);
}
function execMascara() {
obj.value=fun(obj.value);
}
function soNumeros(v) {
var v = this;
if(v.indexOf('.')==-1) {
v = v.replace(/([\d]+)/, "$1,00");
}
v = v.replace(/([\d]+)\.([\d]{1})/g, "$1,$20");
v = v.replace(/([\d]+)\.([\d]{2})/g, "$1,$2");
return v ? "R$ " + v : 'Grátis';
}
</script>
<form>
<label="numero">
Só número: <input id="numero" onkeypress="mascara(this,soNumeros)"/>
</label>
</form>
</body>
</html>