Question

I'm building a form that uses JQuery.Validator with XRegexp to clean input fields before they are submitted to the server (which also validates all incoming posts) but I'm having a little bit of trouble posting the content back to the server.

If I post a UTF-8 string like this: Русский日本語fdsffsfsa my JQuery/Validate/XRegExp POST's the string as UTF-16: xyzzy=%D0%A0%D1%83%D1%81%D1%81%D0%BA%D0%B8%D0%B9%E6%97%A5%E6%9C%AC%E8%AA%9Efdsffsfsa

I know from German Rumm's response on this post Why is jQuery POSTing in UTF-16? that the problem was coming from escape(), but I'm not using it as you can see from my script below:

<script>
$.validator.addMethod(
    "regex",
    function(value, element, regexp) {
        var re = new XRegExp(regexp);
        return this.optional(element) || re.test(value);
    },
    "Please check your input."
);
$(document).ready(function() {
    $("#xyzzyForm").validate({
        rules: {
            xyzzy: {
                required: true,
                minlength: 8,
                maxlength: 32,
                regex: '^[\\p{L}\\p{M}*+\\p{N}]*$'
            }
        },
        messages: {
            xyzzy: {
                required: "Please enter your xyzzy.",
                maxlength: "The xyzzy field only accepts between 8-32 characters.",
                minlength: "The xyzzy field only accepts between 8-32 characters.",
                regex: "The xyzzy field contains invalid characters.  This field only accepts letters and/or numbers."
            }
        },
        submitHandler: function(form) {
            var request;
            if (request) request.abort();
            var $inputs = $(form).find("input, select, textarea, button");
            var serializedData = $(form).serialize();
            $inputs.prop("disabled", true);
            request = $.ajax({
                url: "./xyzzy-ajax.php",
                cache: false,
                type: "post",
                contentType: "application/x-www-form-urlencoded; charset=utf-8",
                data: serializedData
            });
            // Prevent default posting of form.
            return false;
        }
    });
});
</script>

Any idea where the re-encode to UTF-16 is occurring in this case?

Was it helpful?

Solution

Ok I figured out the problem, little embarrassed to say I was led astray but comments regarding JQuery UTF-8 content being encoded to UTF-16 in another post but that wasn't the problem.

Turns out the UTF-8 characters are simply being raw url encoded. All I need to do to decode them on my PHP side is run them through rawurldecode() first then pass it on to my filters for further testing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top