Post

Rails validation form

By HTML5 and Javascript

eg, a submit form
Using setCustomValidity function by default js and oninput="checkPasscode(); , required on input field.

1
2
3
4
5
6
7
8
9
<form>
	<label for="passcode">Enter Passcode:</label>
	<input id="passcode" 
		   type="password" 
		   placeholder="Your passcode" 
		   oninput="checkPasscode();"
		   required/>
	<button type="submit">Submit</button>
</form>
1
2
3
4
5
6
7
8
9
10
function checkPasscode() {
	var passcode_input = document.querySelector("#passcode");
	
	if (passcode_input.value != "Ivy") {
		passcode_input.setCustomValidity("Wrong. It's 'Ivy'.");
	} else {
		passcode_input.setCustomValidity(""); // be sure to leave this empty!
		alert("Correct!");
	}
}
This post is licensed under CC BY 4.0 by the author.