validation on textbox

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2008
Posts: 7
Reputation: karabela is an unknown quantity at this point 
Solved Threads: 0
karabela's Avatar
karabela karabela is offline Offline
Newbie Poster

validation on textbox

 
0
  #1
Jul 29th, 2008
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.

  1. private void yearTextBox_Validating(object sender, CancelEventArgs e)
  2. {
  3. }

i think it should be something like this

  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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2
Reputation: Ramraj@TY is an unknown quantity at this point 
Solved Threads: 0
Ramraj@TY Ramraj@TY is offline Offline
Newbie Poster

Re: validation on textbox

 
0
  #2
Jul 30th, 2008
i think you can set validation in design page itself as Range validator ,

  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
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 7
Reputation: karabela is an unknown quantity at this point 
Solved Threads: 0
karabela's Avatar
karabela karabela is offline Offline
Newbie Poster

Re: validation on textbox

 
0
  #3
Jul 30th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 513
Reputation: selvaganapathy is an unknown quantity at this point 
Solved Threads: 89
selvaganapathy's Avatar
selvaganapathy selvaganapathy is offline Offline
Posting Pro

Re: validation on textbox

 
0
  #4
Jul 30th, 2008
First Parse the Text into DateTime object. Then compare the DateTime objects

  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.
KSG
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 7
Reputation: karabela is an unknown quantity at this point 
Solved Threads: 0
karabela's Avatar
karabela karabela is offline Offline
Newbie Poster

Re: validation on textbox

 
0
  #5
Jul 31st, 2008
when i add your code like this to the Validating event from yearTextBox.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.
  1. DateTime textDateTime = DateTime.Parse(sDateTime);
String was not recognized as a valid DateTime.

what am i doing wrong?
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 513
Reputation: selvaganapathy is an unknown quantity at this point 
Solved Threads: 89
selvaganapathy's Avatar
selvaganapathy selvaganapathy is offline Offline
Posting Pro

Re: validation on textbox

 
0
  #6
Jul 31st, 2008
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
  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. }
  1. Input : 2008
  2. Output : No Message Box
  3.  
  4. Input :2011
  5. Output: MessageBox "It is greater than 2 year"
KSG
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC