JSFiddle - React, Tailwind, and code Playground
by Lalji Tadhani
HTML
<script src="https://code.jquery.com/jquery-3.4.1.slim.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.css">
<form id="business_register_form">
<div class="form-group">
<label for="password">Password:*</label>
<div class="input-group">
<span class="input-group-addon">
<i class="fa fa-lock"></i>
</span>
<input class="form-control" placeholder="Password" required="" name="password" type="password" value="" id="password" aria-required="true" aria-invalid="false">
<button id="toggle-password" type="button" class="" aria-label="Show password as plain text. Warning: this will display your password on the screen.">
</button>
</div><label id="password-error" class="error" for="password" style="display: none;"></label>
<input type="submit" value="Send">
</div>
</form>
JavaScript
document.getElementById("toggle-password").classList.remove("d-none");
const passwordInput = document.querySelector("[type='password']");
const togglePasswordButton = document.getElementById("toggle-password");
togglePasswordButton.addEventListener("click", togglePassword);
function togglePassword() {
if (passwordInput.type === "password") {
passwordInput.type = "text";
togglePasswordButton.setAttribute("aria-label", "Hide password.");
} else {
passwordInput.type = "password";
togglePasswordButton.setAttribute(
"aria-label",
"Show password as plain text. " +
"Warning: this will display your password on the screen."
);
}
}
$('form#business_register_form').validate({
errorPlacement: function(error, element) {
if (element.parent('.input-group').length) {
error.insertAfter(element.parent());
} else if (element.hasClass('input-icheck') && element.parent().hasClass('icheckbox_square-blue')) {
error.insertAfter(element.parent().parent().parent());
} else {
error.insertAfter(element);
}
},
rules: {
name: 'required',
password: {
required: true,
minlength: 5,
},
});
})