date validation

fox.heart.lucille30

New Member
Messages
3
Reaction score
0
Points
0
can someone help me
i need a code/script for date validation either php or javascript

the user cant enter a date that already has passed by or the current date
example if today is oct 1 2010
and the user entered oct 1 2010 or sept 22 2010, the system should not accept the input and display an error

and the user cant enter a date that is invalid like sept 34, 2004
 

bhupendra2895

New Member
Messages
554
Reaction score
20
Points
0
This is programming help section and if you need help then you should write some of the code here you wrote to solve this problem.How you can expect us to write entire code for you for free. :)

See date() in php.net, it should be helpful to you.
 

fox.heart.lucille30

New Member
Messages
3
Reaction score
0
Points
0
This is programming help section and if you need help then you should write some of the code here you wrote to solve this problem.How you can expect us to write entire code for you for free. :)

See date() in php.net, it should be helpful to you.
Oh sorry
I thought I can beg a code here for free, guilty for not reading the rules
Im still trying to search on the net
I'll try date() then :)
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
Dates are tricky things. Well, not dates as such, but the eighteen thousand different ways that users have come up with to express dates as text. Most Americans will use the format "mm/dd/yyyy" or "m/d/yyyy"; the English and some English Canadians tend to use "dd/mm/yyyy" (the rest of English Canada has gotten rather used to the default US settings for Windows); French Canadians and most Europeans will use "yyyy-mm-dd" (though some will use "yyyy.mm.dd" just to annoy programmers); and some people just plain like to spell out the month, either in full or abbreviated and may decide to throw the weekday in just for kicks. For the sake of your own sanity, pick one and only one format that you'd be willing to accept, and enforce that decision with an iron fist.

As for validation, you want both JavaScript and PHP, not one or the other. JavaScript validation happens immediately in the browser, so the user doesn't have to wait for several seconds while your server-side code decides whether the values they entered are acceptable. It's much more user-friendly to give them immediate feedback when you can. (There are some things that can only be validated at the server. You can tell whether a field has been filled in or not, but you have to ask the server if the value is a duplicate, for instance.) But JavaScript validation is only a user convenience -- there is no guarantee that the user has JavaScript enabled, or that s/he might not be the type to fire up a JS debugger and skip through your validation for some reason (usually malicious or, in the case of corporate apps, in order to "time travel" and sign off on something they were supposed to have done last week). So you need to do real validation at the server as well.

In both cases, you are going to want to do two things. First, you'll want to check the value the user enters against a regular expression to make sure that it meets your desired format. Then you'll want to pull the text apart into its component parts (the day, the month, and the year) and create a real date object from those values. Both the JavaScript constructor "new Date()" and the PHP function "mktime()" let you build a date using parts. If you don't use any time components (just the day, month and year, with zeros in the hour, minute and second slots), the date you create will be at the stroke of midnight at the beginning of the day. You can compare that against a simple value (new Date() with no arguments in JavaScript, date() in PHP). Actually writing the code to do that would require knowing what date format you are using.

Give it a shot, and if you have problems, post your code. We'll be happy to help.
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Define what you mean by "code"
Is this a homework assignment?
Is this something you want to put on a webpage and validate either by javascript or PHP?
Do you want the code for an input form?
 
Top