Member Avatar
feoperro

Hi,

I made my own javascript method that I'd like to integrate into the JQuery validation plugin. I am new to JQuery and I'm not really sure how to integrate it though... Here is my javascript working method:

function doAjax() {
                var captchaResponse = document.getElementById("registerCaptcha").value;
                $.ajax({
                    type: "POST",
                    url: "register_captcha",
                    data: "captchaResponse=" + captchaResponse,
                    success: function(data) {
                        alert(data);
                    }
                });
              }

The only ways I know possible are:

1. Change the above code to use the remote method, which I've tried but isn't working for some reason:

$("#registerCaptcha").rules("add", {
                    required: true,
                    remote: {
                        url: "register_captcha",
                        type: "post",
                        data: "captchaResponse=" + captchaResponse,
                        success: function(data) {
                            alert(data);
                        }
                    },
                    messages: {
                        required: "Please complete the captcha",
                        remote: jQuery.format("{0} is incorrect.")
                    }
                });

2. Using the add method, which I'm not sure how to do, but something along the lines of the below which validates a text input and ensures that alpha characters have been entered:

jQuery.validator.addMethod("alpha", function(value, element) {
                    return this.optional(element) || value == value.match(/^[a-zA-Z]+$/);
                },"Only alphabetic characters are allowed.");

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.