-1

I call a script function in the 'Save' button to display an alert when the particular text fields are empty.

Script:

function validateAddRootCaseForm() {

    if (document.getElementById(GetClientId("txtRoot"))) {
        if (document.getElementById(GetClientId("txtRoot")).value == "") {
            alert("Root is mandatory");
            return false;
        }
    }


    if (document.getElementById(GetClientId("txtDes"))) {
        if (document.getElementById(GetClientId("txtDes")).value == "") {
            alert("Description is mandatory");
            return false;
       }
    }

} 

Front-end Code:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent"   runat="Server">
    <table class="style1">
        <tr>
            <td class="style2">
                <asp:Label ID="lblRootCaseID" runat="server" Text="Root Cause ID"></asp:Label>
            </td>
            <td class="style6">
                <asp:TextBox ID="txtRootCaseID" runat="server" Height="25px" Width="260px" ReadOnly="true"></asp:TextBox>
            </td>
            <td>
                &nbsp;
            </td>
        </tr>
        <tr>
            <td class="style2">
                <asp:Label ID="lblRootDes" runat="server" Text="Root">   </asp:Label>
            </td>
            <td class="style6">
                <asp:TextBox ID="txtRoot" runat="server" Height="25px"  Width="260px"></asp:TextBox>
            </td>
            <td>
                &nbsp;
            </td>
        </tr>
        <tr>
            <td class="style2">
                <asp:Label ID="lblDes" runat="server" Text="Description">   </asp:Label>
            </td>
            <td class="style6">
                <asp:TextBox ID="txtDes" runat="server" TextMode="MultiLine" Rows="10" Width="260px"></asp:TextBox>
            </td>
             <td>
                &nbsp;
            </td>
        </tr>
        <tr style="visibility: hidden">
            <td>
            </td>
        </tr>
         <tr style="visibility: hidden">
             <td>
            </td>
        </tr>
        <tr>
            <td class="style5">
            </td>
            <td style="height: 30px">
                <asp:Button ID="btnSave" runat="server" Text="Save" OnClientClick="return validateAddRootCaseForm()"
                Width="80px" Height="25px" OnClick="btnSave_Click" />
                <asp:Button ID="btnAddNew" runat="server" Text="Add New Root Case" Width="150px" Height="25px"
                OnClick="btnAddNewRootCase_Click" />
                <br />
                <br />
                <br />
            </td>
        </tr>
</table>

But, when I click the 'Save' button with empty fields, these alerts do not display. Can someone please help me with this issue.

NKN
  • 6,482
  • 6
  • 36
  • 55
Madushi
  • 1
  • 2
  • 5
  • 5
    What happens when you debug the JavaScript in your browser? Is the function invoked at all? Does it find the elements you're looking for? Where specifically does this fail? – David Mar 29 '15 at 12:21
  • 1
    Please create a demo that replicates problem. We don't know what `GetClientId()` does. Not much troubleshooting information given either – charlietfl Mar 29 '15 at 12:28
  • [How can I debug my JavaScript code?](http://stackoverflow.com/questions/988363/how-can-i-debug-my-javascript-code) – Liam Mar 30 '15 at 09:48

1 Answers1

-1

Try to remove the "return" from the button call

gilix
  • 545
  • 10
  • 14