The onblur event is for when you take the focus off the field and you’re putting it on the button,
you are also passing button value and not inputs value.
One way to work is to place the checkNumber(this.value) function in the onblur event of each input.
An example working the way your question:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Título da pagina</title>
<script>
function checkNumber(value) {
if (value.trim() !== "") {
var regra = /^[0-9]+$/;
if (value.match(regra)) {
alert("Numero: " + value);
}
else {
alert("Permitido somente números");
}
}
}
</script>
</head>
<body>
<input type="text" name="idade" id="idade" onblur="checkNumber(this.value)"/>
</body>
</html>
An example validating several fields containing a certain class:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Título da pagina</title>
<script>
function validaCamposSomenteNumero() {
var inputs = document.getElementsByClassName("somente-numero");
for (var i = 0; i < inputs.length; ++i) {
var input = inputs[i];
if (input.value !== "") {
if (isNaN(input.value)) {
alert("Preencha somente número!");
return;
}
}
}
}
</script>
</head>
<body>
<input type="text" name="idade" id="idade" class="somente-numero"/>
<input type="text" name="idade2" id="idade2" class="somente-numero"/>
<input type="button" value="Cadastrar" onclick="validaCamposSomenteNumero()"/>
</body>
</html>
An example where you want a field to have exactly 4 numbers, age for example, using HTML5.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Título da pagina</title>
</head>
<body>
<form action="" method="post">
<input type="text" name="idade" id="idade" pattern="^[0-9]{4}$" title="Preencha a idade corretamente."/>
<input type="submit">
</form>
</body>
</html>
Allowing a field to contain only numbers using HTML5:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Título da pagina</title>
</head>
<body>
<input type="number" name="idade" id="idade"/>
</body>
</html>
Answering your question of how to enable another field if a given field is valid, I used the disabled property of html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Título da pagina</title>
<script>
function validaCampo(value) {
if (value.trim() !== "") {
var campoParaHabilitar = document.getElementById("campo2");
if (!isNaN(value.trim())) {
campoParaHabilitar.disabled = false;
}
else {
campoParaHabilitar.disabled = true;
}
}
}
</script>
</head>
<body>
<input type="text" name="campo1" id="campo1" onblur="validaCampo(this.value)"/>
<input type="text" name="campo2" id="campo2" disabled/>
</body>
</html>
The onblur event is for when you take the focus off the field and you’re putting it on the button, you’re also passing the value of the button and not the inputs. One way to work is to place the checkNumber(this.value) function in the onblur event of each input.
– Diego Schmidt
You mean like Diego? <label class="label">Humidity: </label> <input type="text" id="Humidity" size="50" value="" onblur="checkNumber(this.value);"> <br>
– cardealt
In addition to what @Diegoschmidt said, I think you could create a variable regexp
var regra =new RegExp("/^[0-9]+$/")
and when validating regexp useregra.test(valor)
– Leonardo Bonetti
Right, just like that. Before you were passing the value of the button and its input.
– Diego Schmidt
But if you are using Html5 why not use input type="number"?
– Don't Panic
@Leonardobonetti: I didn’t understand very well..
– cardealt
@Everson: I have no idea.. As I said, I know very little web language, so I came to you myself..
– cardealt
@cardealt I described in my reply how is the input syntax of type number (number ) and a solution with javascript to restrict the field to numbers only.
– Leandro