i must validate a form before submit, in this code I use routes, so i can’t use $_SERVER[“PHP_SELF”] (maybe) someone can help me please?
JavaScript
x
<form name="user" action="index.php?action=save-report" method="post">
<div class="form-group">
<label for="report">Quante persone ci sono secondo te? </label>
<br>
<input type="number" id="report" name="report" maxlength="180" style="width: 5%" class="form-control" required >
</div>
<br>
<button class="btn btn-primary">Invia</button>
<input type="hidden" id="user__token" name="user[_token]" value="">
<br>
</form>
Thank you
Advertisement
Answer
The best way to validate without refreshing is Javascript, just add a onchange attribute to the input and validate through js.
JavaScript
<input type="text" onchange="validate(this)" required>
<input type="submit" id="submit" value="Invia">
function validate(elem) {
if (elem.value != "Ciao") {
elem.classList.add("notValid"); //for example red border or red label...
document.getElementById("submit").disabled = true;
else {
elem.classList.remove("notValid");
document.getElementById("submit").disabled = false;
}
}