I want a simple program that shows an alert if a user updates a form and it's not more than 8 charterers long. I've tooled around trying to use the "onformchange" function and such, but I couldn't get it to work. I've heard about ajax but don't know how to integrate it into my problem. Any help would be great.
2 Answers
please,use the following guide to validate your form: W3Schools
2 Comments
Seb
That is not the effect I am looking for, I want it to happen before I click anything. The second the user updates the form, it is validated.
Mohamed Magdy
each input has some events you can bind to fire the validation function such as blur,change .
document.getElementById("submit").onclick = function() {
document.getElementById("output").innerHTML = "";
if (document.getElementById("input").value.length < 8) {
document.getElementById("output").innerHTML = "input too short";
}
}
document.getElementById("input").onkeydown = function() {
document.getElementById("output").innerHTML = "";
if (document.getElementById("input").value.length < 8) {
document.getElementById("output").innerHTML = "input too short";
}
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<input type="text" placeholder="some text" id="input">
<input type="button" onclick="click()" value="submit" id="submit">
<p id="output"></p>
</body>
</html>
This will work, make sure to change the id's.
2 Comments
Seb
This does work, but it works on click I want it to happen as the user is going through the form. So if he is done the first input, and progress is to the second, he'll then realise he cant use that username. Is it even possible to do it?
Jojo
Yes! It is, ill add it to the answer in a second