| | |
validation on textbox
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
im a beginner in C sharp. im stuck with the following. i hope someone can get me on the track again.
i want to add a validation for user input such as making sure the year of the yearTextBox is not greater than the current year +2.
i think it should be something like this
current year + 2
but i cant figure out how to put this.
i want to add a validation for user input such as making sure the year of the yearTextBox is not greater than the current year +2.
C# Syntax (Toggle Plain Text)
private void yearTextBox_Validating(object sender, CancelEventArgs e) { }
i think it should be something like this
C# Syntax (Toggle Plain Text)
if (yearTextBox.Text >= DateTime.......
current year + 2
but i cant figure out how to put this.
Last edited by karabela; Jul 29th, 2008 at 8:42 pm.
•
•
Join Date: Jul 2008
Posts: 2
Reputation:
Solved Threads: 0
i think you can set validation in design page itself as Range validator ,
the max n min values only have change
C# Syntax (Toggle Plain Text)
<asp:Label ID="lbl" runat="server"></asp:Label> <asp:TextBox ID="txt" runat="server"></asp:TextBox> <asp:RangeValidator id="rv" runat="server" ControlToValidate="txt" MaximumValue="2008" MinimumValue="2003" ErrorMessage="Range Exceeded"></asp:RangeValidator>
the max n min values only have change
Last edited by Tekmaven; Jul 30th, 2008 at 9:50 am. Reason: Code tags
the situation is as follows. i have a database. one table with the name listing. one of the column name is year.
i created a form with datagridview. when i enter new information it must validate the user input. i think this validation has to be created on form level. this validation must check the year is not greater than the current year + 2.
its not range validation what im looking for.
i created a form with datagridview. when i enter new information it must validate the user input. i think this validation has to be created on form level. this validation must check the year is not greater than the current year + 2.
its not range validation what im looking for.
First Parse the Text into DateTime object. Then compare the DateTime objects
C# Syntax (Toggle Plain Text)
string sDateTime = "20/8/2010"; // yearTextBox.Text DateTime textDateTime = DateTime.Parse(sDateTime) ; DateTime compareDateTime = DateTime.Now.AddYears (2); if (textDateTime.CompareTo(compareDateTime) >= 0) { MessageBox.Show("It is greater"); }
Last edited by selvaganapathy; Jul 30th, 2008 at 1:31 pm.
KSG
when i add your code like this to the Validating event from yearTextBox.Text
i get format exception. String was not recognized as a valid DateTime.
what am i doing wrong?
C# Syntax (Toggle Plain Text)
private void yearTextBox_Validating(object sender, CancelEventArgs e) { string sDateTime = yearTextBox.Text; DateTime textDateTime = DateTime.Parse(sDateTime); DateTime compareDateTime = DateTime.Now.AddYears(2); if (textDateTime.CompareTo(compareDateTime) >= 0) { MessageBox.Show("It is greater"); } }
i get format exception.
C# Syntax (Toggle Plain Text)
DateTime textDateTime = DateTime.Parse(sDateTime);
what am i doing wrong?
I think the Format of the Date is not correct or It may contains invalid date so the exception happens.
If you give the input as "20/8/2010", is this working?
I think year is the only the Input and Not Date, like "20/8/2010". Just want to check only year?
If so, try this
If you give the input as "20/8/2010", is this working?
I think year is the only the Input and Not Date, like "20/8/2010". Just want to check only year?
If so, try this
C# Syntax (Toggle Plain Text)
int givenYear = int.Parse( yearTextBox.Text ); int currentYear = DateTime.Today.Year ; if ( givenYear > currentYear + 2 ) { MessageBox.Show("It is greater than 2 year"); }
C# Syntax (Toggle Plain Text)
Input : 2008 Output : No Message Box Input :2011 Output: MessageBox "It is greater than 2 year"
KSG
![]() |
Similar Threads
- Want Date Format in dd/mm/yyyy in ASP.Net With C#. (C#)
- Validation help (Visual Basic 4 / 5 / 6)
- for validation - text box and drop down menu (JavaScript / DHTML / AJAX)
- CustomValidate, another problem (ASP.NET)
- Validation difficulties *sigh* (ASP.NET)
- Validation function (Visual Basic 4 / 5 / 6)
- validating a textbox without using validation control (ASP.NET)
- Javascript, Form fields validation and submit (JavaScript / DHTML / AJAX)
Other Threads in the C# Forum
- Previous Thread: COM Interop Question
- Next Thread: Unable to save last item in listbox
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access algorithm array barchart bitmap box buttons c# chat check checkbox class client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees draganddrop drawing encryption enum event excel file files form format forms ftp function gdi+ httpwebrequest image index input install java label list listbox listener login mandelbrot math mouseclick mysql networking object operator oracle path photoshop picturebox pixelinversion post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml





