What is the error in this regular expression?

Asked

Viewed 100 times

2

What is the error of this regular expression. I have tried it with another function and the mask works. But this one for pennies, it doesn’t work. What’s the mistake?

<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>
  • @rray has already been formatted. I’m still interacting with stackoverflow formatting.

  • Remove the . exhaust from both regex, tested without them and worked normally.

  • @Diego Felipe is to take out the symbols "" and "."? I took out the "" and continues accepting letters.

  • 1

    Look at @Brunno’s reply, it fits perfectly to what you want to do.

  • @Diego Felipe Vi, but has a situation, I commented there. The user needs to enter 00 to enter values only in real. If you do not enter 00, the value is changed.

  • 1

    I see no problem in it, if you check big internet bankings, these digits work this way, you type and need to add zeros to "grow" a value. As long as the mask shows the user that if they type 120, it will format as 1.20 before (as in the Runner solution), it is no problem. One should pay attention to the case that it turns off javascript, but I believe that a confirmation screen would be enough.

  • @Diego Felipe true. I will use so and make a note in the form for the user.

Show 2 more comments

2 answers

2

The object window is overwriting the value entered in the text field, see line 16.

var v = this; //Está instrução não faz muito sentido

The object window does not have the function indexOf().

  • put with the changes cited by you. Check, please, in Pastebin: http://pastebin.com/JTf3PRJ5

2

To add the mask only with Javascript and Regex, I would do as follows:

document.getElementById('numero').addEventListener("keyup", mascara); //Pego evento de keyup ()

function mascara() {
    var v = this.value; //Pego valor
    v = v.replace(/\D/g, ''); //Valida se é numero
    v = v.replace(/(\d{2})$/, ',$1'); //Seto valores antes da virgula
    v = v.replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1.');
    this.value = v != '' ? 'R$ ' + v : 'Grátis'; //Retorno seu valor com (R$) ou se estiver vazio seta para (Grátis) :D 
}

Follows a jsfiddle of the mask :)

  • worked, but has a detail: if I type 120 real, will return me 1,20. The function has to accept the comma or the point typed by the user. If the user enters another character other than the comma, it does not accept in the field. If type automatically, or the user type 00 at the end or will change the value.

  • It has a character "*" which is called Lazy, something like that to find a character in the edit field.

  • @Andrénascimento, then face this example above he will put the mask incrementally, in my application he met legal, but it is possible some changes so that accept point or comma more will have to change a little the functioning of the regex .. Another case would be to use some plugin, there are several that do it :D More than not want to put jQuery in the middle ai worth analyzing the change in regex hehehe'

  • 1

    Brunno @ I preferred the regex, I only made some changes, because when I typed 1000, the correct would be to structure for 10.00, but the regex put the comma like: 100.0. I had part of the regex that inserted the "," after 3 numbers. I only got the last line, which is to insert "," before the last two numbers.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.