JSFiddle - React, Tailwind, and code Playground
by aalfiann
HTML
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/native-form-validation@1.2.1/dist/formvalidation.min.js"></script>
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Form Signup</title>
</head>
<body>
<h1>Form Signup</h1>
<form>
<div id="email_group" class="form-group">
<label for="email">Email address</label>
<input id="email" type="email" class="form-control" aria-describedby="emailHelp" placeholder="Enter email">
<small id="email-error" class="form-text text-danger"></small>
</div>
<div id="password_group" class="form-group">
<label for="password">Password</label>
<input id="password" type="password" class="form-control" placeholder="Password">
<small id="password-error" class="form-text text-danger"></small>
</div>
<div id="confirm_password_group" class="form-group">
<label for="confirm_password">Confirm Password</label>
<input id="confirm_password" type="password" class="form-control" placeholder="Confirm Password">
<small id="confirm_password-error" class="form-text text-danger"></small>
</div>
<div id="checkbox_group" class="form-check">
<input id="checkbox" type="checkbox" class="form-check-input">
<label class="form-check-label" for="exampleCheck1">Check me out</label>
<small id="checkbox-error" class="form-text text-danger"></small>
</div>
<button id="submit_signup"...
JavaScript
var FV = new FormValidation();
FV.rules({
email: {
required:true,
regex: /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/,
errorPlace:'email-error',
errorAddClass: {
email_group:'has-danger',
email:'is-invalid'
}
},
password: {
required: true,
message: 'Password must be min 3-20 chars!',
minLength:3,
maxLength:20,
errorPlace:'password-error',
errorAddClass: {
password_group:'has-danger',
password:'is-invalid'
}
},
confirm_password: {
required: true,
message: 'Confirm Password must be min 3-20 chars!',
minLength:3,
maxLength:20,
errorPlace:'confirm_password-error',
errorAddClass: {
confirm_password_group:'has-danger',
confirm_password:'is-invalid'
}
},
checkbox: {
message: 'You have to check this first!',
method: function(element) {
return element.checked;
},
errorPlace:'checkbox-error',
errorAddClass: {
checkbox_group:'has-danger',
checkbox:'is-invalid'
}
}
});
function checkMatchPass() {
if (FV.element('password').isValid() && FV.element('confirm_password').isValid()) {
if($('#password').val() !== $('#confirm_password').val()) {
FV.element('password').setCustomError('Password is not match!');
FV.element('confirm_password').setCustomError('Password is not match!');
} else {
FV.element('password').reset();
FV.element('confirm_password').reset();
}
} else {
FV.element('password').validate();
FV.element('confirm_password').validate();
}
}
$(document).ready(function(){
$('#email').on('blur', function() {
FV.element(this.id).validate();
});
$('#password').on('blur', function() {
checkMatchPass();
});
$('#confirm_password').on('blur', function() {
...