0

I am validating a field remotely and I am getting the following error:

Uncaught TypeError: Cannot read property 'call' of undefined.
jquery.validate.js:617 
n.extend.checkjquery.validate.js:423
n.extend.elementjquery.validate.js:271
n.extend.defaults.onfocusoutjquery.validate.js:366
ijquery.validate.js:1359  
(anonymous function)jquery-2.0.3.js:4676  
i.event.dispatchjquery-2.0.3.js:4360
i.event.add.y.handlejquery-2.0.3.js:4594
i.event.triggerjquery-2.0.3.js:4893  
i.event.simulatejquery-2.0.3.js:5009  
i.support.focusinBubbles.i.each.f   

Here is my code:

 $("input[id$=txtCompanyName]").rules("add", {
                required: true,
                alphanumeric: true,
                remote: function() {
                    return {
                        url: "/Resources/wsResources.asmx/IsCompanyAvailable",
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        data: JSON.stringify({ company: $("input[id$=txtCompanyName]").val() }),
                        dataFilter: function(data) {
                            console.log(data);
                            var msg = JSON.parse(data);
                            if (msg.hasOwnProperty('d'))
                                return msg.d;
                            else
                                return msg;
                        }
                    };
                },
                messages: {
                    required: "This field is required",
                    alphanumeric: "Company name is not in correct format",
                    remote: $.validator.format("{0} already exists")
                }
            });
0

2 Answers 2

1

I think you may be making a mistake in the PHP (or your remote code). I was and I just worked it out. If you are writing:

if(foo){echo true;}
else {echo false;}

Try writing:

if(foo){echo "true";}
else {echo "false";}

It worked for me. Have a look at the response to your ajax call in the console to see what's actually being returned.

Sign up to request clarification or add additional context in comments.

Comments

0
  • The remote method is already defined with a function() contained inside the plugin. You would not define a new function in place of the expected object literal. In other words, simply follow the documentation and list the options inside of the remote method.

  • If you are going to apply the .rules() method to more than one element using the jQuery "ends with" selector, then you must enclose .rules() within a jQuery .each().

  • Make sure you are including the additional-methods.js file when you're using the alphanumeric rule.

  • Many of your options are the defaults and can be left out. The data of the field is already sent by default. The dataType is already JSON, etc.

  • You would not write any code to handle the server response yourself. The remote method is already programmed to capture & handle the server response. Your server-side script simply needs to respond with a true, false or JSON string.

  • You would not define a custom message for remote using the {0} parameter placeholder since there is no parameter. If your server-side code returns a JSON string, the field is made invalid and that string automatically becomes the error message. Otherwise, you can define a static message for remote within messages.

  • We cannot see your HTML or your call to .validate(). However, verify that you call .validate() once to initialize the plugin on your form; and the form already exists when you call .validate(). You must also have a unique name attribute on every field element or the plugin will fail.

Again, please refer to the documentation for remote.

$("input[id$=txtCompanyName]").each(function() {
    $(this).rules("add", {
        required: true,
        alphanumeric: true,
        remote: {
            url: "/Resources/wsResources.asmx/IsCompanyAvailable",
            type: "POST"
        },
        messages: {
            required: "This field is required",
            alphanumeric: "Company name is not in correct format"
        }
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.