0

I have the following code but it doesn't fire or doesn't show any error when I click on the save button. Could you please let me know what could be the problem? Thank you.

<script type="text/javascript">
$(document).ready(function () {
  $('input[id*=btnSave]').click(function () {
    var txtFirstName = $('input[id*=txtFirstName]').val();
    var txtLastName = $('input[id*=txtLastName]').val();
    var errMsg = '';
    var errMsg = "<ul>";
    if (txtFirstName == '') {
      errMsg = errMsg + "<li>First Name is required</li>";
      return false;
    }
    if (txtLastName == '') {
      errMsg = errMsg + "<li>Last Name is required</li>";
      return false;
    }
    errMsg = errMsg + "</ul>";
  });
});
</script>

<telerik:RadTextBox ID="txtLastName" runat="server" />
<telerik:RadTextBox ID="txtFirstName" runat="server"/>
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click"></asp:Button>
2
  • where is btnSave_Click function Commented May 24, 2012 at 19:34
  • that means you want to implement multiple process on submit button click? Commented May 24, 2012 at 19:37

4 Answers 4

1

I think you selector line needs to have quotes around the id value:

$('input[id*="btnSave"]').click(function () {
Sign up to request clarification or add additional context in comments.

Comments

1

I think

$('input#btnSave').click(function() {

}); 

is enough.

don't need to use

$('input[id*=btnSave]')

and where is your btnSave_Click function

Comments

0

Try this:

<script type="text/javascript">
           $(document).ready(function () {
                $('#btnSave').click(function () {

                    var txtFirstName = $('#txtFirstName').val();
                    var txtLastName = $('#txtLastName').val();
                    var errMsg = '';
                    var errMsg = "<ul>";
                    if (txtFirstName == '') {
                        errMsg = errMsg + "<li>First Name is required</li>";
                        return false;
                    }
                    if (txtLastName == '') {
                        errMsg = errMsg + "<li>Last Name is required</li>";
                        return false;
                    }
                errMsg = errMsg + "</ul>";
                });
            });
    </script>

Comments

0

It works when I use

$('#<%= btnSave.ClientID %>').click(function ()

1 Comment

the <asp:Button> is going to generate <input type='sumbit' ...> but the id is going to be set to something like "ctl100_btnSave" depending upon how deeply the button itself is nested in other server controls, like master pages and so on. the reason the example in the answer works is because the ClientID property is the value you set (btnSave) and matches the variable name in the code behind. Your original code would work, I think your wildcarding is wrong is all.

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.