How to add jquery ajax validation using the remote rule
I will use the jQuery ajax validation to use the remote rule in Laravel to show you how it can be solved and a perfect example for you it completely error-free.
resources/views/index.blade.php
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/jquery.validate.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/additional-methods.min.js"></script>
<form id="users_form" class="form-horizontal form-label-left" enctype="multipart/form-data">
<h3>How to add jquery ajax validation using the remote rule</h3>
<input type="hidden" id="id" name="id">
<input type="hidden" id="action" name="action">
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Name<span class="required">*</span></label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input type="text" class="form-control col-md-7 col-xs-12 name" id="name" placeholder="Enter Name" name="name" />
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Email<span class="required">*</span></label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input type="email" class="form-control col-md-7 col-xs-12 email" id="email" placeholder="Enter Email" name="email" />
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Password<span class="required">*</span></label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input type="password" class="form-control col-md-7 col-xs-12 password" id="password" placeholder="Enter Password" name="password" />
<input type="hidden" name="password_old" class="password_old">
</div>
</div>
<div class="form-group">
<div class="col-md-offset-3 col-md-6 col-sm-offset-3 col-sm-6 col-xs-12">
<button type="submit" class="btn btn-success btn-submit"> Save</button>
</div>
</div>
</form>
<script type="text/javascript">
jQuery.validator.addMethod("alphanumeric", function(value, element) {
return this.optional(element) || /^[a-zA-Z]+$/.test(value);
});
$("#users_form").validate({
rules: {
name: {
required: true,
minlength: 3,
maxlength: 25,
alphanumeric:true,
remote: {
type: 'POST',
url: '{{ url('users_username_check') }}',
data: {
name: function() {
return $('#users_form :input[name="name"]').val();
},
id: function() {
return $('#users_form :input[name="id"]').val();
}
}
},
},
email: {
required: true,
email: true,
remote: {
url: '{{ url('users_email_check') }}',
type: "post",
data: {
email: function() {
return $('#users_form :input[name="email"]').val();
},
id: function() {
return $('#users_form :input[name="id"]').val();
}
}
},
},
password: {
required: true,
minlength: 6
},
user_role: "required",
},
messages: {
name: {
required: "Please Enter Your Name",
minlength: "Username Minimum 3 latter",
maxlength: "Username Maximum 25 latter",
alphanumeric: "Letters only please",
remote: "This username allready register!"
},
email: {
required: "Please Enter Your Email",
email: "Enter Proper Email Address!",
remote: "This email address allready register!"
},
password: {
required: "Please Enter Your Password",
minlength: "Please Enter Minimum 6 digit insert!"
},
},
});
</script>
routes/web.php
Route::post('users_email_check','UsersController@usersemailcheck');
Route::post('users_username_check','UsersController@usersusernamecheck');
app/Http/Controller/UsersController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use DB;
use Validator;
use Response;
class UsersController extends Controller
{
public function usersemailcheck(Request $request) {
$id = $request->id;
$email = $request->email;
if(!empty($id)) {
$rules = array('email' => 'required|email|unique:users,email,'. $id.',id');
} else {
$rules = array('email' => 'required|string|email|unique:users');
}
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
echo "false";
} else {
echo "true";
}
/* Or */
if (!empty($request->email)) {
$email = $request->email;
$id = $request->id;
if(!empty($id)) {
$users_count = User::where('email',$email)->where('id','!=',$id)->count();
} else {
$users_count = User::where('email',$email)->count();
}
if($users_count == 0) {
echo "true";
} else {
echo "false";
}
} else {
echo "false";
}
}
public function usersusernamecheck(Request $request) {
$id = $request->id;
$name = $request->name;
if(!empty($id)) {
$rules = array('name' => 'required|unique:users,name,'. $id.',id');
} else {
$rules = array('name' => 'required|unique:users');
}
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
echo "false";
} else {
echo "true";
}
/* Or */
if (!empty($request->name)) {
$name = $request->name;
$id = $request->id;
if(!empty($id)) {
$users_count = User::where('name',$name)->where('id','!=',$id)->count();
} else {
$users_count = User::where('name',$name)->count();
}
if($users_count == 0) {
echo "true";
} else {
echo "false";
}
} else {
echo "false";
}
}
}
Good tutorial.