nulling function action if result is false

2

I have the following function: if the entered data is NaN, it gives focus to the field and changes the border

function validaNumero(){
	dado = document.getElementById("new_partner");
	var formulario = document.getElementById("new_partner");
	var dado = formulario.partner_registry_number.value;
		if(isNaN(dado)) {
			formulario.partner_registry_number.style.focus;
			formulario.partner_registry_number.style.border = "2px solid red";
			return false;
		} 
	}
So far so good, but after the first one did that the function is accessed, the bar always turns red, even typing a valid data. How do I resolve this?

    
asked by anonymous 21.12.2016 / 19:56

1 answer

2

Each time the code is executed and the value is NaN, it will change the border to red. After the value is no longer NaN, you have to bring the border back to the original state.

You only need a else .

if(isNaN(dado)) {
    formulario.partner_registry_number.style.border = "2px solid red";
} else {
    formulario.partner_registry_number.style.border = ""; // borda original
}

Or to optimize the code ..

formulario.partner_registry_number.style.border = isNaN(dado) ? '2px solid red' : '';
    
21.12.2016 / 20:59