Jquery verification plug-in validationplugin remote verification

Source: Internet
Author: User
Tags php server
When using the jquery validation plug-in validationplugin for remote verification, the document says that the server needs to return a json string. true indicates that the parameter is valid. & quot; false & quot;, undefined, and null indicates that the parameter is invalid. Verify whether the user name has been registered. If the user name has been registered, return... when using the jquery validation plug-in validation plugin for remote verification, the document says that the server needs to return a json string. true indicates valid, "false", undefined, and null indicates invalid.
Verify whether the user name has been registered. If the user name has been registered, false is returned. If the user name has not been registered, true is returned. The php server (laravel framework) is written as follows:

    public function verifyName(Request $request)    {        $username = $request->input('name', '');        $user = User::where('name', $username)->first();        if ($username == $user->name) {            return json_encode(false);        }else{            return json_encode(true);        }    }

The front-end js Code is as follows:

《script》    $(function () {        $.ajaxSetup({            headers: {                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')            }        });        $("#register").validate({            rules: {                name: {                    required: true,                    rangelength: [4, 30],                    remote: {                        url: "{{ url('services/validation/verify_name') }}",                        type: "post"                    }                }            },            errorClass: "has-danger",            validClass: "has-success",            //success: "valid",            highlight: function (element, errorClass) {                $(element).fadeOut(function () {                    $(element).fadeIn();                });                $(element).closest(".form-group").addClass(errorClass);            },            unhighlight: function (element, errorClass) {                $(element).closest(".form-group").removeClass(errorClass);            },            errorPlacement: function (error, element) {                error.insertAfter(element);            }        });    });《script》

Problem:
If 'false' is returned, it can be returned normally. If you want to return 'true', it cannot be returned. Trying to get property of non-object appears. I do not know why.

Reply content:

When using the jquery validation plug-in validation plugin for remote verification, the document says that the server needs to return a json string. true indicates valid, "false", undefined, and null indicates invalid.
Verify whether the user name has been registered. If the user name has been registered, false is returned. If the user name has not been registered, true is returned. The php server (laravel framework) is written as follows:

    public function verifyName(Request $request)    {        $username = $request->input('name', '');        $user = User::where('name', $username)->first();        if ($username == $user->name) {            return json_encode(false);        }else{            return json_encode(true);        }    }

The front-end js Code is as follows:

《script》    $(function () {        $.ajaxSetup({            headers: {                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')            }        });        $("#register").validate({            rules: {                name: {                    required: true,                    rangelength: [4, 30],                    remote: {                        url: "{{ url('services/validation/verify_name') }}",                        type: "post"                    }                }            },            errorClass: "has-danger",            validClass: "has-success",            //success: "valid",            highlight: function (element, errorClass) {                $(element).fadeOut(function () {                    $(element).fadeIn();                });                $(element).closest(".form-group").addClass(errorClass);            },            unhighlight: function (element, errorClass) {                $(element).closest(".form-group").removeClass(errorClass);            },            errorPlacement: function (error, element) {                error.insertAfter(element);            }        });    });《script》

Problem:
If 'false' is returned, it can be returned normally. If you want to return 'true', it cannot be returned. Trying to get property of non-object appears. I do not know why.

《script》    $(function () {        $.ajaxSetup({            headers: {                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')            }        });        $("#register").validate({            rules: {                name: {                    required: true,                    rangelength: [4, 30],                    remote: {                        url: "{{ url('services/validation/verify_name') }}",                        type: "post",                        data:{                            name:function(){                                return $("input[name='name']").val();                            }                        }                    }                }            },            errorClass: "has-danger",            validClass: "has-success",            //success: "valid",            highlight: function (element, errorClass) {                $(element).fadeOut(function () {                    $(element).fadeIn();                });                $(element).closest(".form-group").addClass(errorClass);            },            unhighlight: function (element, errorClass) {                $(element).closest(".form-group").removeClass(errorClass);            },            errorPlacement: function (error, element) {                error.insertAfter(element);            }        });    });《script》

Did your front-end code forget to post data to the background?
Data :{

name:function(){    return $("input[name='name']").val();}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.