0

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';
    }
}
Theodore
  • 49
  • 2
  • 7
  • don't echo, just return `true` or `false` – Wreigh Jan 11 '18 at 02:32
  • I'm afraid that doesn't help when i check the network in chrome it is still giving me a 419 server error – Theodore Jan 11 '18 at 02:58
  • you're missing something for your *ajax* request. see this: https://stackoverflow.com/questions/46472812/ajax-laravel-419-post-error?answertab=oldest#tab-top – Wreigh Jan 11 '18 at 03:01
  • I'm not sure how to add the csrf token in the header I'm getting a uncaught syntax error unexpected token: – Theodore Jan 11 '18 at 03:14
  • add it as first line of the script. and if you have new codes in question, update your question; including the new codes. – Wreigh Jan 11 '18 at 03:28
  • I did some research about the csrf token and found the error that was giving me the problem laravel always generates a token whenever a post request is made and always checks it. I found out how to correct it using meta tags and ajax. I must say thanks for your comments guided me to towards a solution. – Theodore Jan 11 '18 at 19:15
  • good for you. you're welcome! :) – Wreigh Jan 11 '18 at 23:12

0 Answers0