1
Hello,
I need some help. I have a field input[type=text] that I have triggers on it, a trigger .change and a .keypress .
However, in the .change i call a field validation function and .keypress i check if user pressed ENTER for me to save in the bank the value of this field.
With this, when the user enters some value in this field and this field is not in agreement, the function of .change points out the problem, but even so the .keypress is triggered and saves the line with the incorrect value.
Is there any way I don’t need to call validation on .keypress also?
Example:
$("campo").change(function(e){
valida_campo(this);
}).keypress(function(e){
if(e.which == 13) {
salva_campo(this);
}
});
The event
changedoes not fire when you insert text. Thisvalida_camposhould not be run in the case ofEnter? This valida_field `is asynchronous?– Sergio
It is not asynchronous. It fires both triggers when you press ENTER.
– Don't Panic
I don’t see the need for you to use the event
change, whereas in thekeypressalready does what you need. Use only thekeypressto validate and save the field, ie will only save, after the function resultvalida_campofortrue, for example.– Douglas Garrido
I agree with @Douglasgarrido, it could be something like this: https://jsfiddle.net/7hxues6L/
– Sergio
Exactly as @Sergio demonstrated.
– Douglas Garrido
Okay, I understand your position and also agree. Problem solved.
– Don't Panic