JSFiddle - React, Tailwind, and code Playground
HTML
<form action="" method="post">
<label>Login Name:</label><br>
<input type="text" name="email" id="log" /><br>
<label>Password:</label><br>
<input type="text" name="password" id="pwd" /><br>
<label>Subject:</label><br>
<input type="text" name="password" id="pwd" /><br>
<label>Message:</label><br>
<textarea rows="5" name="message" cols="50" id="message"></textarea><br>
<input type="submit" name="submit" value="Login" />
</form>
JavaScript
// Bind the event handler to the "submit" JavaScript event
$('form').submit(function () {
// return true only if all inputs have text in them, false if otherwise
return Array.prototype.reduce.call(
// first argument is the 'this value', we need an array
// map over the array of inputs, check if empty or not, returns array of trues and falses
$('input, textarea').map(function(i, x){
// Check if empty of not
var text = $.trim($(x).val());
if (text == '') {
alert($(x).attr('name') + ' is empty. All inputs are required.');
return false;
} else {
return true;
}
}),
// the reduce function, true && false == false
function(a,b){
return a && b
},
// initial value
true);
});