I have tested past Sitepoint scripts that it should be checked Terms before form is submitted. How to make also SUBMIT button as grey color when form is not complete and when validation is positive it will make it green? As I understand it will be possible to press ENTER and form will be submitted. Example is for an email input and checkbox.
Example from past posts:
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="UTF-8">
<title>Form submission block</title>
</head>
<body>
<script>
var form = document.querySelector('form#MyformID');
form.addEventListener('submit', function (e)
{
var someCheckBox = document.querySelector('#read');
if (!someCheckBox.checked) {
e.preventDefault();
document.querySelector('#message').innerHTML = 'You have to read the terms';
}
}
);
</script>
<form id="form#MyformID"action="">
<input name="emailinput1" class="" placeholder="" style="" type="email">
<label for="read">
<p>I have read the terms:</p>
<input id="read" type="checkbox">
</label>
<input type="submit" style="grey">
<div id="message"></div>
</form>
The following example uses built-in HTML5 validation by placing required on the checkbox. https://jsfiddle.net/pmw57/mjog1bkj/1/
The only thing that the script does, is to add/remove a class from the submit button, allowing you to style the submit button.
When you attempt to submit without ticking the have-read checkbox, the built-in form validation shows a validation message. Details on form validation are helpful there.
A general rule though is don’t trust anything that comes through from the browser. You cannot guarantee that a submitted form has the checkbox ticked, so use backend server code as your primary validation technique instead, checking what was submitted from the form.
Will this work also as logical operator and request also email to be filled in and not empty?
I have seen many bot abuses and form is submitted using inputs that execute form as submitted.
Can be used this script as additional measure to prevent abuse and input field will not be if email is empty and checkbox is not checked or we missed email input as there can be many inputs as empty string. I have also PHP control regarding empty strings.
I have added bot control and if empty value is detected it will block to send form inputs. I’m not sure if this is working as form is submitted even this measure is implemented.