JSFiddle - React, Tailwind, and code Playground
HTML
<form class="formulier" method="post" action="#">
<input type="text" maxlength="20" name="naam" id="naam">
<label for="naam">Naam</label><br>
<div class="naam_error"><br></div><br>
<input type="url" maxlength="50" name="url" id="url">
<label for="url">URL</label><br>
<div class="url_error"><br></div><br>
<input type="email" maxlength="50" name="email" id="email">
<label for="email">Email</label><br>
<div class="email_error"><br></div><br>
<input type="text" maxlength="50" name="onderwerp" id="onderwerp">
<label for="onderwerp">Onderwerp</label><br>
<div class="onderwerp_error"><br></div><br>
<textarea name="content" id="content" rows="18" cols="50">
Geachte Meneer/mevrouw,
Dit is een test
Ik hoop snel van jullie terug te horen!
Met vriendelijke groeten,
test
</textarea><br>
<div class="content_error"><br></div><br>
<input type="submit" value="verzenden" id="submit_form">
</form>
JavaScript
$("#submit_form").click(function(event){
//validate process
var submitnaam = $("input#naam").val();
var submiturl = $("input#url").val();
var submitemail = $("input#email").val();
var submitonderwerp = $("input#onderwerp").val();
var submitcontent = $("textarea#content").val();
var valid = true;
//validate realistic length
if(submitnaam.length < 3)
{
$('div.naam_error').html('Name is too short.');
valid = false;
}
else
{
$('div.naam_error').html('<br>');
}
if(submiturl.length < 5)
{
$('div.url_error').html('URL is too short.');
valid = false;
}
else
{
$('div.url_error').html('<br>');
}
if(submitemail.length < 8)
{
$('div.email_error').html('Email is too short.');
valid = false;
}
else
{
$('div.email_error').html('<br>');
}
if(submitonderwerp.length < 3)
{
$('div.onderwerp_error').html('Subject is too short.');
valid = false;
}
else
{
$('div.onderwerp_error').html('<br>');
}
if(submitcontent.length < 3)
{
$('div.content_error').html('Content is too short.');
valid = false;
}
else
{
$('div.content_error').html('<br>');
}
if(valid)
{
//submit form
var datastring = 'name='+ submitnaam + '&url=' + submiturl + '&email=' + submitemail + '&onderwerp=' + submitonderwerp + '&content=' + submitcontent;
$.ajax({
type: "POST",
url: "submit.php",
data: datastring,
success: function() {
...