I am trying to validate my form using jQuery validation js but the problem is I'm not receiving any error message in the toastr.
I followed this link for implementing the error in toastr. The error I am getting is 'This field is required' below the text box. Below is my code for the same:
HTML
<form class="card-body">
<div class="form-row">
<div class="col-md-6 mb-3">
<label for="validationDefault01">First name</label>
<input type="text" class="form-control" id="validationDefault01" name="firstName">
</div>
<div class="col-md-6 mb-3">
<label for="validationDefault02">Last name</label>
<input type="text" class="form-control" id="validationDefault02" name="lastName">
</div>
</div>
<div class="form-row">
<div class="col-md-6 mb-3">
<label for="validationDefault03">City</label>
<input type="text" class="form-control" id="validationDefault03" name="city">
</div>
<div class="col-md-3 mb-3">
<label for="validationDefault04">State</label>
<select class="custom-select" id="validationDefault04" name="state">
<option selected disabled value="">Choose...</option>
<option>...</option>
</select>
</div>
<div class="col-md-3 mb-3">
<label for="validationDefault05">Zip</label>
<input type="text" class="form-control" id="validationDefault05" name="zip">
</div>
</div>
<div class="row">
<div class="col-md-3 mb-3">
<label for="username">Username</label>
<input type="text" class="form-control" id="username" name="username">
</div>
<div class="col-md-3 mb-3">
<label for="password">Password</label>
<input type="text" class="form-control" id="password" name="password">
</div>
</div>
<div class="form-group">
<div class="form-check">
<input class="form-check-input" type="checkbox" id="invalidCheck2" name="confirm">
<label class="form-check-label" for="invalidCheck2">
Agree to terms and conditions
</label>
</div>
</div>
<button class="btn btn-success" type="submit" id="submitBtn">Submit</button>
<button class="btn btn-danger" type="reset">Reset</button>
</form>
JS
function toasterOptions() {
toastr.options = {
"closeButton": false,
"debug": false,
"newestOnTop": false,
"progressBar": true,
"positionClass": "toast-top-right",
"preventDuplicates": true,
"onclick": null,
"showDuration": "100",
"hideDuration": "1000",
"timeOut": "5000",
"extendedTimeOut": "1000",
"showEasing": "swing",
"hideEasing": "linear",
"showMethod": "show",
"hideMethod": "hide"
};
};
$(document).ready(function () {
toasterOptions();
$('form').submit(function(e) {
e.preventDefault();
$(this).validate({
rules: {
firstName: 'required',
lastName: 'required'
},
messages: {
firstName: () => {
toastr.error('First Name required')
},
lastName: () => {
toastr.error('Last Name required')
}
}
});
var values = $(this).serializeArray();
var xyz = {};
$.each(values, function (i, obj) {
xyz[obj.name] = obj.value
});
window.localStorage.setItem('data', JSON.stringify(xyz));
toastr['info']('Done');
});
});
Is there anything am missing out ?