Click here to Skip to main content
16,017,322 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi to all,

I am working on formview control.
I wrote everything for inserting data by using textboxes in formview control but now I want to validate textboxes.

I wrote javascript for validations not to accept blank values into textboxes.

I have taken two buttons in <Insert itemtemplate> and whenever I press the close button I want to validate my textbox.

I have created a funtion as below.
Please any one can help me.
Thanks in advance

XML
<script id="CloseNotesFormView" language="javascript" type="text/javascript">
                function validate() {
                       if (document.getElementById("<%=NotesTextBox.ClientID%>").value == "") {
                           alert("You cant close the issue without Note");
                           document.getElementById("<%=NotesTextBox.ClientID%>").focus();
                           return false;
                       }
                       else {
                           alert("Issue has been successfully closed");
                           return true;
                       }
                                     }
               </script>



.aspx

VB
<asp:Button ID="btnUpdCloseNote" runat="server" CausesValidation="True"
                           Text="Close" Height="21px" Width="60px" onclick="btnUpdCloseNote_Click" OnClientClick="return validate" xmlns:asp="#unknown"
                           ValidationGroup="CloseNote" />

XML
<td class="style33" valign="top">
                        Close Note</td>
                    <td>
                        <asp:TextBox ID="NotesTextBox" runat="server" TextMode="MultiLine" Rows="6"
                            Font-Names="&quot;Lucida Grande&quot;,Arial,Lucida,Helvetica,sans-serif"
                            Font-Size="11px" Width="304px"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="rqfCloseNote" runat="server"
                            ErrorMessage="Close Note must be entered"
                            ControlToValidate="NotesTextBox" Display="Dynamic"
                            ValidationGroup="CloseNote">*</asp:RequiredFieldValidator>
                    </td>
Posted
Updated 23-Feb-11 21:47pm
v3
Comments
Dalek Dave 24-Feb-11 3:48am    
Edited for Grammar, Syntax and Readability.

1 solution

Don't validate black values, simply filter them out, so the user cannot enter them.

For example:

HTML
<html>
    <head>
        <script type="text/javascript"><!--
            function filterBlank(eventInstance) {
                eventInstance = eventInstance || window.event;
                    key = eventInstance.keyCode || eventInstance.which;
                if (key != 32) {
                    return true;
                } else {
                        if (eventInstance.preventDefault)
                             eventInstance.preventDefault();
                        eventInstance.returnValue = false;
                        return false;
                    } //if
            } //filterBlank
        --></script>
    </head>
<body>
<input type="text" onkeypress="filterBlank(event)"/>
</body>
</html>



—SA
 
Share this answer
 
v3
Comments
Dalek Dave 24-Feb-11 3:48am    
Good Call.
Sergey Alexandrovich Kryukov 24-Feb-11 3:48am    
Thank you.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900