Richa
Richa

Reputation: 407

Client Side Validation

I have a field on an asp.net page for which I have used RequiredFieldValidator.

<tr> 
  <td align="left" style="width: 130px"> 
    <asp:Label ID="LblPagename" runat="server" Text="Page Name " CssClass="label"></asp:Label><font 
      color="red"> *</font> 
  </td> 
  <td align="left" style="width: 1087px" class="layout-grid"> 
    <asp:TextBox ID="TxtPageName" runat="server" MaxLength="50" ></asp:TextBox> 
    <asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" Display="Dynamic" 
      ValidationGroup="Save" ControlToValidate="TxtPageName" ForeColor="Red"
      ErrorMessage="Please enter Page Name."></asp:RequiredFieldValidator>  
  </td> 
</tr>

on save I am allowing CauseValidation="true" for the the same ValidationGroup as mentioned above

<tr> 
  <td colspan="3" align="center" style="width: 1193px"> 
    <asp:ImageButton ID="btnMenu" CausesValidation="true" ValidationGroup="Save" ImageUrl="~/Images/Save.png"  
      runat="server" OnClick="btnMenu_Click" OnClientClick="SaveFunctionalities();"/> 
    &nbsp;&nbsp; &nbsp;&nbsp; 
    <asp:ImageButton ID="btnCancelMenu" ImageUrl="~/Images/btnCancel.png" runat="server" 
      OnClick="btnCancelMenu_Click" /> 
  </td> 
</tr>

But on save, along with the error message for the empty field, the save is being called.

The same thing is working fine on other pages.

The only difference is that here I have a javascript function call on OnClientClick.

Is that causing some problem?

Upvotes: 1

Views: 134

Answers (1)

freefaller
freefaller

Reputation: 19953

Make sure that SaveFunctionalities(); returns a boolean value, depending on whether an error has occured or not. If everything is fine, then return true - if there are any errors return false.

Then update your OnClientClick to pass that return variable to the button..

OnClientClick="return SaveFunctionalities();"

If there are no problems, the button will continue as normal via the true value. If there are errors, then it will be told to stop processing via the false value.

UPDATE

From the code posted in the comment by @Richa, you are always returning false, no matter what the validation returns.

Your function should look more like this...

function SaveFunctionalities() {
  if (Page_ClientValidate()) {
    ...
    return true;
  } else {
    return false; 
  }
}

Upvotes: 1

Related Questions