jQuery Validator Plugin - check for existing Username/Email in mysql database

后端 未结 3 1523
天涯浪人
天涯浪人 2020-12-16 01:49

I\'ve successfully created a form that submits and adds users to a mysql database, and form validation with \'jQuery Validator\' plugin works great for everything except che

3条回答
  •  青春惊慌失措
    2020-12-16 02:20

    $.validator.addMethod("checkExists", function(value, element)
    {
        var inputElem = $('#register-form :input[name="email"]'),
            data = { "emails" : inputElem.val() },
            eReport = ''; //error report
    
        $.ajax(
        {
            type: "POST",
            url: validateEmail.php,
            dataType: "json",
            data: data, 
            success: function(returnData)
            {
                if (returnData!== 'true')
                {
                  return '

    This email address is already registered.

    '; } else { return true; } }, error: function(xhr, textStatus, errorThrown) { alert('ajax loading error... ... '+url + query); return false; } }); }, '');

    OR

    You can use the remote method instead which allows you to do remote checks: http://docs.jquery.com/Plugins/Validation/Methods/remote

    Eg.

        $("#yourFormId").validate({
                rules: {
                    email: {
                        required: true,
                        email: true,
                        remote: {
                            url: "checkUnameEmail.php",
                            type: "post"
                         }
                    }
                },
                messages: {
                    email: {
                        required: "Please Enter Email!",
                        email: "This is not a valid email!",
                        remote: "Email already in use!"
                    }
                }
            });
    

    checkUnameEmail.php //Eg.

        
    

提交回复
热议问题