Jquery validation with Laravel blade file :
jquery_validation_demo.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery-validation@1.19.5/dist/jquery.validate.min.js"></script>
<style>
.error{
color:red;
}
.basic-form{
display:flex;
justify-content:center;
}
</style>
</head>
<body>
<div class="mb-3 row text-center">
<h1>USER INFO</h1>
</div>
<div class="row basic-form">
<div class="col-md-6">
<form class="dz-form pb-3" action="{{ route('saveUserData') }}" method="POST" name="userForm" id="userForm">
@csrf
<h3 class="form-title m-t0">Personal Information</h3>
<div class="dz-separator-outer m-b5">
<div class="dz-separator bg-primary style-liner"></div>
</div>
<div class="form-group mb-3">
<input type="text" class="form-control" placeholder="Name" name="name" id="name" value="">
</div>
<div class="form-group mb-3">
<input type="email" class="form-control" placeholder="Email" name="email" id="email" value="">
</div>
<div class="form-group mb-3">
<input type="password" class="form-control" placeholder="Password" name="password" id="password" value="">
</div>
<div class="form-group mb-3">
<input type="password" class="form-control" placeholder="Password" name="confirm_password" id="confirm_password" value="">
</div>
<div class="form-group text-center mb-5 forget-main">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
</div>
</div>
<script>
$(function() {
$('#userForm').validate({
rules: {
name: {
required: true,
minlength: 5
},
email: {
required: true,
email: true,
remote: {
url: "{{ route('checkEmailUnique') }}",
type: 'POST',
data: {
_token: '{{ csrf_token() }}',
}
}
},
password: {
required: true,
minlength: 8
},
confirm_password: {
required: true,
minlength: 8,
equalTo: '#password'
}
},
messages: {
name: {
required: "Please enter your username",
minlength: "Your username must be at least 5 characters long"
},
email: {
required: "Please enter your email address",
email: "Please enter a valid email address",
},
password: {
required: "Please provide a password",
minlength: "Your password must be at least 8 characters long"
},
confirm_password: {
required: "Please confirm your password",
minlength: "Your password must be at least 8 characters long",
equalTo: "Passwords do not match"
}
}
});
});
</script>
</body>
</html>
checkEmailUnique function to validate uniqueness of email with php Laravel
function checkEmailUnique(Request $request)
{
$email = $request->input('email');
$check_email_exist = User::where('email', $email)->first();
if ($check_email_exist){
return response()->json('This Email is already taken...!!');
} else {
return response()->json(true);
}
}