0

I am using jQuery remote validation with asp.net codebehind and webservice, now the remote function is hitting the server fine and I am returning True or false JSON response but what is happening is the following:

The first time I hit the Login button with intentional empty field I get an error message both on client side and server side, fine but if I fill the field and try to hit the login button the Remote function is working fine but not client side and the error message is not going even though the field is filled.

I have tried many searches on net with no luck, I would deeply appreciate it if someone could help on this.

Here is my code:

  $("#Login").click(function () {
                 $("#form1").validate(
                 // This prevents validation from running on every
                 //  form submission by default.
                 //  onsubmit: false

{ rules: { Email: { required: true,

     remote: {
        type: "POST",
        url: "WebForm1.aspx/IsValid",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{'username':'" + function () {
            return $("#UserName").val();
        } + "'}", success: function (msg) {
            result = (msg == '1') ? true : false;
         //   alert(msg.d); 
        }
    }


}
}
}
              );

                 var isValid = $("#form1").valid();



               alert(isValid);
             });

server side code

     public static Boolean IsValid()
    {

        return true;
    }
1
  • Are you meaning to have 2 data: fields in your remote: rules ? Commented Nov 22, 2010 at 17:52

1 Answer 1

1

Ok well it doesn't seem to be affecting your outcome, but you do have 2 data: fields.

Besides that, you are treating the remote: rule like a regular jquery ajax call... its not like that. It doesn't have a success callback, etc... It should look more like this.

remote: {
        type: "post",
        url: "WebForm1.aspx/IsValid",
        data: { username: function () {
            return $("#UserName").val();
                                      }
              }
        }

And your server side well echo "true" or "false" based on the posted value being true or false, it's not done via a success callback.

So something like

 public static Boolean IsValid()
{

  document.write("true");
}

Basically, it will hit the url: you give it, pass it the variables in data: and wait for a true or false to determine if its valid code or not. The reason your form validate works when you put nothing in, is because of the required: true value you have checks if its empty...

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

2 Comments

man thanks alot for the hint, how stupid was I! you were right about the Email Rule, i forgot and changed it, i may be wrong but from Jquery manual it says server should return JSON response and i am using code behind webmethod and it reruns response in form of {"d":false}, there is no document.write("true"); in C#. so please can you elaborate on this
Sorry, Response.Write. Whatever is going to return just a simple true or false in plain text to the ajax request

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.