943,603 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 13705
  • C# RSS
Jul 29th, 2008
0

validation on textbox

Expand Post »
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.

C# Syntax (Toggle Plain Text)
  1. private void yearTextBox_Validating(object sender, CancelEventArgs e)
  2. {
  3. }

i think it should be something like this

C# Syntax (Toggle Plain Text)
  1. 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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
karabela is offline Offline
7 posts
since Jul 2008
Jul 30th, 2008
0

Re: validation on textbox

i think you can set validation in design page itself as Range validator ,

C# Syntax (Toggle Plain Text)
  1. <asp:Label ID="lbl" runat="server"></asp:Label>
  2. <asp:TextBox ID="txt" runat="server"></asp:TextBox>
  3. <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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ramraj@TY is offline Offline
2 posts
since Jul 2008
Jul 30th, 2008
0

Re: validation on textbox

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
karabela is offline Offline
7 posts
since Jul 2008
Jul 30th, 2008
0

Re: validation on textbox

First Parse the Text into DateTime object. Then compare the DateTime objects

C# Syntax (Toggle Plain Text)
  1. string sDateTime = "20/8/2010"; // yearTextBox.Text
  2.  
  3. DateTime textDateTime = DateTime.Parse(sDateTime) ;
  4. DateTime compareDateTime = DateTime.Now.AddYears (2);
  5. if (textDateTime.CompareTo(compareDateTime) >= 0)
  6. {
  7. MessageBox.Show("It is greater");
  8. }
Last edited by selvaganapathy; Jul 30th, 2008 at 1:31 pm.
Reputation Points: 44
Solved Threads: 101
Posting Pro
selvaganapathy is offline Offline
547 posts
since Feb 2008
Jul 31st, 2008
0

Re: validation on textbox

when i add your code like this to the Validating event from yearTextBox.Text

C# Syntax (Toggle Plain Text)
  1. private void yearTextBox_Validating(object sender, CancelEventArgs e)
  2. {
  3.  
  4. string sDateTime = yearTextBox.Text;
  5.  
  6. DateTime textDateTime = DateTime.Parse(sDateTime);
  7. DateTime compareDateTime = DateTime.Now.AddYears(2);
  8. if (textDateTime.CompareTo(compareDateTime) >= 0)
  9. {
  10. MessageBox.Show("It is greater");
  11. }
  12.  
  13. }

i get format exception.
C# Syntax (Toggle Plain Text)
  1. DateTime textDateTime = DateTime.Parse(sDateTime);
String was not recognized as a valid DateTime.

what am i doing wrong?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
karabela is offline Offline
7 posts
since Jul 2008
Jul 31st, 2008
0

Re: validation on textbox

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
C# Syntax (Toggle Plain Text)
  1. int givenYear = int.Parse( yearTextBox.Text );
  2.  
  3. int currentYear = DateTime.Today.Year ;
  4. if ( givenYear > currentYear + 2 )
  5. {
  6. MessageBox.Show("It is greater than 2 year");
  7. }
C# Syntax (Toggle Plain Text)
  1. Input : 2008
  2. Output : No Message Box
  3.  
  4. Input :2011
  5. Output: MessageBox "It is greater than 2 year"
Reputation Points: 44
Solved Threads: 101
Posting Pro
selvaganapathy is offline Offline
547 posts
since Feb 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Finding active(focused) window -urgent-
Next Thread in C# Forum Timeline: Unable to save last item in listbox





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC