开发者

Anchor tag validation

开发者 https://www.devze.com 2023-01-11 20:00 出处:网络
I am trying 开发者_如何学编程to make an anchor tag cause both client and server validation. I have this code for now:

I am trying 开发者_如何学编程to make an anchor tag cause both client and server validation. I have this code for now:

$(document).ready(function () {

              $('div#imgEmailVerifyLoader').hide();

              $('a#btn_SubmitContactMessage').click(function () 
              {
                  if (Page_ClientValidate()) // this will trigger all validators on page  
                  {
                      $('div#imgEmailVerifyLoader').show('slow');
                      window.Form_OnMasterPage.submit();
                      return true;
                  }
                  else 
                  {
                      return false;
                  }

              });

          });  

<a id="btn_SubmitContactMessage" href="Contact.aspx"  onclick="Validate();" runat="server">SUBMIT</a> 

This performs client validation properly and shows the error message. I have validation controls for each of the textboxes on the page. I also added a server click event handler in code behind for this:

 btn_SubmitContactMessage.ServerClick +=new EventHandler(btn_SubmitContactMessage_ServerClick);


        }

        protected void btn_SubmitContactMessage_ServerClick(object sender, EventArgs e)
        {
            if (!Page.IsValid)
            {

                RequiredFieldValidator4.ErrorMessage = "show";
                return;
            }
        }

But when I try to test it by turning off javascript the link(submit) does not postback. Why is that happening?

Now, how do I make sure that validation is being done on the server side to after postback.


I would imagine it's because of the 'onclick=validate()'. Instead of doing that you should register that event inside of '$(document).ready(function ()' like you've got your other JavaScript. That way if JavaScript is not available the form is submitted normally and your server side validation kicks in.

0

精彩评论

暂无评论...
验证码 换一张
取 消