Click here to Skip to main content
15,906,816 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I want validate textbox to accept date only and other validate to accept integers only...in C#.net windows application..so please help me...send me above validation code....please...
Posted
Comments
Sergey Alexandrovich Kryukov 27-May-11 11:25am    
Tag it! WPF, Forms, ASP.NET, what?!
--SA

I think the better solution is DateTimePicker[^].
 
Share this answer
 
Comments
Kim Togo 27-May-11 17:28pm    
5 for the Calendar control - DateTimePicker.
If you want to edit just date, don't use TextBox. Instead, use MaskedTextBox or even better, one of the Calendar controls (I cannot tell more exactly what before you tag what UI library you want to use).

—SA
 
Share this answer
 
v2
Comments
Kim Togo 27-May-11 11:35am    
My 5 for Calendar controls.
Sergey Alexandrovich Kryukov 27-May-11 11:35am    
Thank you, Kim.
--SA
Sanjay J Patolia 28-May-11 3:08am    
Yes agreed. 4
Sergey Alexandrovich Kryukov 28-May-11 3:14am    
Thank you.
--SA
Use MaskedTextBox control not ordinary TextBox you can find it in the Toolbox, then use mask property and chose date from it.

Take care.
 
Share this answer
 
v4
Comments
Sergey Alexandrovich Kryukov 27-May-11 11:34am    
I decided to vote 4 for this answer but...
Shame on you! I had to fix your spelling, formatting, capitalization and punctuation to make your text looking in acceptable way. Hope you don't mind? You really need to put it all correctly; it's just a matter of politeness. Also, "use mask property" is not clear or accurate -- does not matter, OP just needs to look at the MSDN help page to see how to use it.

Also, please see my answer.
--SA
Kim Togo 27-May-11 11:35am    
My 5 for MaskedTextBox. Good :-)
For integer validation you can use ajax filtered textbox extender. like,-
<cc1:FilteredTextBoxExtender ID="FilteredTextBoxExtender4"  runat="server" FilterType="Numbers"
                                                            TargetControlID="txtNo">
                                                        </cc1:FilteredTextBoxExtender>


And for Date you can use calender control extender. Like,-
<asp:TextBox id="txtPayDate" runat="server" Width="287px" MaxLength="64" BorderColor="LightGray" BorderStyle="Solid" BorderWidth="1px"></asp:TextBox>
                                                                                    
                                                                                    <asp:RequiredFieldValidator id="RequiredFieldValidator16" runat="server" ValidationGroup="AddUser" ErrorMessage="Enter Payment Date" Display="None" SetFocusOnError="True" ControlToValidate="txtPayDate"></asp:RequiredFieldValidator>
                                                                                    
                                                                                    <cc1:ValidatorCalloutExtender id="ValidatorCalloutExtender16" runat="server" TargetControlID="RequiredFieldValidator16"></cc1:ValidatorCalloutExtender>
                                                                                    <cc1:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtPayDate"
                                                            Format="dd MMM yyyy"></cc1:CalendarExtender>
                                                        <cc1:ValidatorCalloutExtender ID="ValidatorCalloutExtender17" runat="server" TargetControlID="RegularExpressionValidator1"></cc1:ValidatorCalloutExtender>
                                                        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtPayDate"
                                                            ErrorMessage="Invalid Format" ValidationExpression="^((31(?!\ (Feb(ruary)?|Apr(il)?|June?|(Sep(?=\b|t)t?|Nov)(ember)?)))|((30|29)(?!\ Feb(ruary)?))|(29(?=\ Feb(ruary)?\ (((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)))))|(0?[1-9])|1\d|2[0-8])\ (Jan(uary)?|Feb(ruary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|Aug(ust)?|Oct(ober)?|(Sep(?=\b|t)t?|Nov|Dec)(ember)?)\ ((1[6-9]|[2-9]\d)\d{2})$"
                                                            ValidationGroup="AddFAQ"></asp:RegularExpressionValidator> 
 
Share this answer
 
v4
hi,,

//accept only numbers
    <script type="text/javascript">;
        function onlyNumbers(event) {
            var e = event || evt; // for trans-browser compatibility
            var charCode = e.which || e.keyCode;
            if (charCode > 31 && (charCode < 48 || charCode > 57))
                return false;
            return true;
        }
    </script>;
//In page load of the aspx page
   txtAcStNo.AddTextBoxAttribute("onkeypress", "return onlyNumbers(event);");
 
Share this answer
 

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