473,395 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Date Validation Question

Stang02GT
1,208 Expert 1GB
I have been asked to validate a date on our web-page so that people cannot enter dates like 14/1/08 or 2/30/06. I have found code that will do exactly what i need it to do, but i am not sure how to call it. It was suggested to be to have it run through an if statement and if the user enters an invalid date it will kick them to an error page.

Here is the date validation code.

Expand|Select|Wrap|Line Numbers
  1. // date validation using SimpleDateFormat
  2. // it will take a string and make sure it's in the proper 
  3. // format as defined by you, and it will also make sure that
  4. // it's a legal date
  5.  
  6. public boolean isValidDate(String date)
  7. {
  8.    // set date format, this can be changed to whatever format
  9.    // you want, MM-dd-yyyy, MM.dd.yyyy, dd.MM.yyyy etc.
  10.    // you can read more about it here:
  11.    // http://java.sun.com/j2se/1.4.2/docs/api/index.html
  12.  
  13.    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
  14.  
  15.    // declare and initialize testDate variable, this is what will hold
  16.    // our converted string
  17.  
  18.    Date testDate = null;
  19.  
  20.    // we will now try to parse the string into date form
  21.    try
  22.    {
  23.      testDate = sdf.parse(date);
  24.    }
  25.  
  26.    // if the format of the string provided doesn't match the format we 
  27.    // declared in SimpleDateFormat() we will get an exception
  28.  
  29.    catch (ParseException e)
  30.    {
  31.      errorMessage = "the date you provided is in an invalid date" +
  32.                              " format.";
  33.      return false;
  34.    }
  35.  
  36.    // dateformat.parse will accept any date as long as it's in the format
  37.    // you defined, it simply rolls dates over, for example, december 32 
  38.    // becomes jan 1 and december 0 becomes november 30
  39.    // This statement will make sure that once the string 
  40.    // has been checked for proper formatting that the date is still the 
  41.    // date that was entered, if it's not, we assume that the date is invalid
  42.  
  43.    if (!sdf.format(testDate).equals(date)) 
  44.    {
  45.      errorMessage = "The date that you provided is invalid.";
  46.      return false;
  47.    }
  48.  
  49.    // if we make it to here without getting an error it is assumed that
  50.    // the date was a valid one and that it's in the proper format
  51.  
  52.    return true;
  53.  
  54. } // end isValidDate
  55.  

I'm not to sure about the syntax of java or how to do this, lol thats why i'm asking you guys, but


if (!Validator.isValidDate(fromDate)) {
LOG.fine("From Date contains an invalid date:" + fromDate);
return mapping.findForward(NOT_FOUND_FORWARD);
}

What i THINK this will do is run the code from the isValidDate method to check the date in the fromDate field, and if it is not right it will kick them to the NOT_FOUND_FORWARD page and tell them that From Date contains an invalid date.


That is pretty much what I want to is check the fromDate and toDate fields and make sure they are valid and if not kick them to an error page.

Hopefully i didn't confuse anyone lol.
Jan 17 '08 #1
5 3418
r035198x
13,262 8TB
If you want the method to be available without creating an instance of its enclosing class then make it static as well and Just call it like you've done above.
Jan 17 '08 #2
Stang02GT
1,208 Expert 1GB
Ok, well now i guess my next question would be there is also a toDate field, is there a way that i can include bother fromDate and toDate in one statement could i start it like this?

Expand|Select|Wrap|Line Numbers
  1. if (!Validator.isValid(fromDate) || !Validator.isValid(toDate))
How can i change the rest of my if statement to include toDate?

I apologize for my lack of knowledge in Java :( I'm just starting to learn it.
Jan 17 '08 #3
BigDaddyLH
1,216 Expert 1GB
Ok, well now i guess my next question would be there is also a toDate field, is there a way that i can include bother fromDate and toDate in one statement could i start it like this?
Expand|Select|Wrap|Line Numbers
  1. if (!Validator.isValid(fromDate) || !Validator.isValid(toDate))
Could you? Have you tried it? I suggest you write small unit tests for your code so that you can see if it works without having to go through the web layer for each test.

Unit Testing
Jan 17 '08 #4
Stang02GT
1,208 Expert 1GB
For this date validation issue. I am still trying to develop this IF statement. I got side tracked with another project and now have time to go back and focus on this.

So if i need to validate this date, i want to check both fields (fromDate, toDate) against the function. If there is nothing wrong with the date then I just do nothing?
Else send them to the error page telling them they entered an invalid date.

I apologize for my ignorance with this but i am trying to learn Java, could you possibly help me to develop this statement and better understand it?

I have the general idea of what i want to do, but i am not sure how to code it.
Jan 24 '08 #5
Stang02GT
1,208 Expert 1GB
Actually I figured it out.

I was thinking too hard about something that was so simple.

Expand|Select|Wrap|Line Numbers
  1. if (!Validator.isValid(fromDate) || !Validator.isValid(toDate)) {
  2.     LOG.fine("From Date and To Date are not the correct format:" + fromDate + ", " + toDate);
  3.     return mapping.findForward(NOT_FOUND_FORWARD);
  4. }
This if statement is checking to see if the dates are "not valid". It translates to - "If fromDate is not valid or toDate is not valid, so i just needed to add my code for the error message and I was done.
Jan 24 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

30
by: Dr John Stockton | last post by:
It has appeared that ancient sources give a method for Numeric Date Validation that involves numerous tests to determine month length; versions are often posted by incomers here. That sort of code...
2
by: Ian | last post by:
I would like to have some validation on a date field. The date format is dd/mm which is used for our financial year end. I suppose I need also consider leap years. Please can you shed some light on...
4
by: peashoe | last post by:
I have an asp page that uses a calendar.js (pop-up) file to add an exact date format in the text field (txtDDate). My problem is I need some javascript that sets an alert that does not allow them...
11
by: Diego | last post by:
Hi all a quick question: how can I validate a date in asp.net (2.0) with c#? I didn't find a quick anwer. Thanks, Diego.
12
by: Diego | last post by:
Can I validate (possibly with a compare validator) a Date entered by the user based upon his regional settings? I.e. if a user is american the format would be mm/dd/yyyy, if brittish dd/mm/yyyy...
8
by: libsfan01 | last post by:
how can i use regular expressions to ensure a mysql format date entry in a text field? thanks marc
17
by: Petyr David | last post by:
Just looking for the simplest. right now my perl script returns an error messge to the user if the date string is invalid. would like to do this before accessing the server. TX
3
by: Harlequin | last post by:
I must start this posing by making the point that I am NOT a VB programmer and I'm something of a Newbie to MS Access. I can program in a number of languages (Java, Javascript, PERL,PHP and TCL) but...
5
by: maz77 | last post by:
I'm trying to develop a good validation for a date in C#; a valid date can be inserted in these formats: - dd/mm/yyyy - dd/mm/yyyy h24:m - mm/dd/yyyy - mm/dd/yyyy h12:m How can I proceed? Is...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.