When using a jQuery form validation script, an "OK!" message is typically displayed below the input field when the input is correct. However, I would like to customize this behavior and have the "OK!" message appear on the right side of the field instead (while keeping the error messages under the field), similar to the layout shown in the image below:
https://i.sstatic.net/E3ApK.jpg
If you want to see a working example, you can check out this fiddle: https://jsfiddle.net/q7nub2v4/1/
Additionally, here's the corresponding HTML code snippet:
<!DOCTYPE html>
<html lang="fr">
<head>
<link rel="stylesheet" type="text/css" href="https://bootswatch.com/cerulean/bootstrap.min.css" />
<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.0.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script type="text/javascript" src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.14.0/jquery.validate.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.14.0/additional-methods.js"></script>
</head>
<form id="registration-form" action="#" method="POST">
<div class="col-sm-5">
<div class="control-group has-feedback">
<label class="control-label" for="username">Username</label>
<div class="controls">
<input type="text" name="username" id="username" class="form-control" placeholder="user"/>
</div>
</div>
<div class="control-group has-feedback">
<label class="control-label" for="email">Email</label>
<div class="controls">
<input type="text" name="email" id="email" class="form-control" placeholder="email"/>
</div>
</div>
<div class="control-group has-feedback">
<label class="control-label" for="password">Password</label>
<div class="controls">
<input type="password" name="password" id="password" class="form-control" placeholder="pass"/>
</div>
</div>
<br>
<button type="submit" class="btn btn-primary">submit</button>
</div>
</form>
<script>
$(document).ready(function () {
$('#registration-form').validate({
rules: {
username: {
rangelength: [6, 16],
pattern:/^[a-zA-Z0-9_-]+$/,
required: true
},
email: {
required: true,
email: true
},
password: {
rangelength: [8, 12],
required: true,
}
},
highlight: function (element) {
$(element).closest('.control-group').removeClass('has-success').addClass('has-error');
},
success: function (element) {
element.text('OK!').addClass('valid')
.closest('.control-group').removeClass('has-error').addClass('has-success');
}
});
});
</script>