Ok so I am using jquery validation plugin to validate my form and it is working good so far my only problem is that i want to check if a username exits while the user is entering data into the form. I have solved the problem already using jquery and plain php. Now i want to do the samething using jquery validation and laravel framework. Here is a sample of my js. As always any help would be appreciated.
var fnameregex = /^[a-z -]+$/i;
var lnameregex = /^[a-z'-]+$/i;
var usernameregex = /^[a-z0-9_'-]+$/i;
var emailregex = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
var passregex = new RegExp("^(?=.*[a-z])(?=.*[A-Z])(?=.{8,})");
var aos = '';
$.validator.addMethod('firstname_regex', function(value,element){
return this.optional(element) || fnameregex.test(value);
},'This name is not valid.')
$.validator.addMethod('lastname_regex', function (value,element) {
return this.optional(element) || lnameregex.test(value);
},'This name is not valid')
$.validator.addMethod('username_regex', function (value,element) {
return this.optional(element) || usernameregex.test(value);
},'This username is not valid')
$.validator.addMethod('password_regex', function (value,element) {
return this.optional(element) || passregex.test(value);
},'Passwords must be eight characters or more contain one uppercase, one numeric number and one lowercase.')
$(function(){
$('#signupform').validate({
rules:{
firstname:{
required:true,
firstname_regex:true,
nowhitespace:true,
minlength:3,
},
lastname:{
required:true,
lastname_regex:true,
nowhitespace:true,
minlength:3,
},
username:{
required:true,
username_regex:true,
nowhitespace:true,
remote:{
url:"checkusers",
type:"post"
}
},
email:{
required:true,
email:true,
nowhitespace:true,
},
password:{
required:true,
password_regex:true,
nowhitespace:true,
},
passconfirm:{
required:true,
password_regex:true,
nowhitespace:true,
equalTo:"#password",
}
},
highlight: function(element) {
$(element).closest('.form-group').addClass('has-error');
$(element).closest('.form-control').css("border-color","#a94442");
},
unhighlight: function(element) {
$(element).closest('.form-group').removeClass('has-error');
$(element).closest('.form-control').css("border-color","#00b8e6");
// $('#firstname').css("border-color","#00b8e6");
},
messages:{
passconfirm:{
equalTo:"Please ensure your passwords match."
},
firstname:{
minlength:"Please enter a valid name."
},
lastname:{
minlength:"Please enter a valid name."
},
username:{
remote: "This username already exits."
}
}
});
})
i sent a post request to checkusers now i want to tell laravel what to do when it receives such a request if the username is already taken the it should return a response of false therefore the remote validation would fail and give the proper error else return true and it passes the remote validation here is a sample of my route in laravel.
Route::post('checkusers',function(){
$user = users::all()->where('username', Input::get('username'))->first();
if ($user) {
echo "false";
} else {
echo "true";
}
});
i will also include a php script i have which does the work i only need someway of doing the same thing in laravel.
require_once "connect.php";
if(isset($_POST['username'])) {
$username = $_POST['username'];
$result =$conn->query("SELECT username FROM users WHERE username='$username'");
if ($result->num_rows ==0){
echo 'true';
}
else {
echo 'false';
}
}