473,387 Members | 1,516 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,387 software developers and data experts.

how to validate user date input

Hi all,

I want to validate on the client side and I have the following php code (tbxBirthDate is the text box, ie name="tbxBirthDate")
Expand|Select|Wrap|Line Numbers
  1.   with(window.document.enrollForm)
  2.   {
  3.     .
  4.     $strBirthDate = tbxBirthDate;
  5.     $arr=split("/",$strBirthDate); // splitting the array
  6.  
  7.     $dd=$arr[0];
  8.     $mm=$arr[1];
  9.     $yy=$arr[2];
  10.   }
  11.   if(!checkdate($mm,$dd,$yy))
  12.   {
  13.     alert("Please enter your birthdate in dd/mm/yyyy fomat");
  14.     cBirthDate.focus();
  15.     return false;
  16.   }
  17.  
I keep getting the run time error "Error: Object expected" and I think it is the line $arr=split("/",$strBirthDate)

Can anyone see what the problem is?

Is this the best way to validate a date?

thanks
Denis
Jun 8 '09 #1
9 5588
prabirchoudhury
162 100+
hey..
have a look on this example , would help you

Javascript Date Validation - mm/dd/yyyy format


:)
Jun 8 '09 #2
Atli
5,058 Expert 4TB
Hi.

Are you mixing JavaScript with PHP?
If you are, then that is never going to work.

PHP is executed server side, before the browser even knows that it is getting Javascript code to execute.

The two can not be mixed together. (Although AJAX can call PHP scripts if needed.)
Jun 8 '09 #3
prabirchoudhury
162 100+
It a date validation and that could be on server side (php get more server load) or on the client side (JavaScript) or could use both (Ajax). He is doing on client side validation using JavaScript. JavaScript and php could work together if the form get posted then

Expand|Select|Wrap|Line Numbers
  1. <script type="text/JavaScript">
  2. <?php
  3.       if (!empty($_POST[tbxBirthDate])){
  4. ?>
  5.  alert('<?php echo $_POST[tbxBirthDate]; ?>');
  6. <?
  7.  }
  8.  ?>
  9.  
  10. </script>
  11.  
Jun 8 '09 #4
Atli
5,058 Expert 4TB
@prabirchoudhury
Not really, no.

PHP can output Javascript for the client to execute (just as it outputs HTML), but the two can't directly work together. Or more specifically, Javascript can't work with PHP. (Except for using AJAX, of course... and even that barely qualifies.)

You can obviously have Javascript submit values via a forum submission, but that is rarely an acceptable method for client-side code.
Jun 8 '09 #5
prabirchoudhury
162 100+
Javascript and PHP they both execute in different place and different way but they could work togather is complately different how they execute. they could share the result and output and that would be in client side..

In Ajax also they execute in different place but from the client to server side xml work with them and make xmlHttp request..

:)
Jun 8 '09 #6
Thanks all for your inputs.

Sorry I haven't responded earlier as I have been using/testing you guy's suggestions.

Your comments made me think and looked at my code again. I was in fact confused between script and php code.

I am now using
Expand|Select|Wrap|Line Numbers
  1.     var strDate = tbxBirthDate.value;
  2.     var arr = strDate.split("/"); // splitting the array
and I found the following function
Expand|Select|Wrap|Line Numbers
  1. <script language="JavaScript">
  2. function isValidDate(dd,mm,yyyy)
  3. {
  4.   var strDate = mm + "/" + dd + "/" + yyyy;
  5.   var dt = new Date(strDate);
  6.  
  7.   if (dt.getDate() != dd)
  8.   {
  9.     return false;
  10.   }
  11.   if (dt.getMonth() != mm - 1)
  12.   {
  13.     // Javascript starts month from 0
  14.     return false;
  15.   }
  16.   if (dt.getFullYear() != yyyy)
  17.   {
  18.     return false;
  19.   }
  20.   return true;
  21. }
  22. </script>
This seems to work. The example in prabirchouhury's link is very complicated. I can't understand the need to eg check year range etc.

thanks to you all
Denis
Jun 9 '09 #7
Dormilich
8,658 Expert Mod 8TB
@dragon52
on some systems the year is saved using 2 digits (originating from times, where memory was a valuable good) so if Date.getYear() returns 04, it could mean 1904 or 2004 now (that was no problem until 2000…).
Jun 9 '09 #8
Atli
5,058 Expert 4TB
The Y2K Problem. Shortsightedness at it's best.
Jun 9 '09 #9
Yes, good point.

I will check for a fixed length of 10. That should stop people entering values such as 12/3/1988 or 10/10/04.

thanks
Jun 10 '09 #10

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

Similar topics

7
by: Alexandre | last post by:
Hi again, What's the best I can do to validate fields like date before send my datas. thx
2
by: Meredith | last post by:
I am using a script to validate a form using the presence of a value in one field and determine if there is a value in one of two fields. It is an either/or situation. If the date rcvd field is...
7
by: James P. | last post by:
Hello there, In my asp.net page using VB, I have a date text field in mm/dd/yyyy format. When a date is entered, I'd like to validate it to make sure the date is greater than or equal to the...
0
by: Marina | last post by:
Let's say a user control has a bunch of textboxes. At some point the user control calls Validate to make sure all input is processed. The Validating event is fired, and if the input was invalid,...
2
by: NishSF | last post by:
Would anyone have any suggestions/javascript code so that if one clicks the Radio Button "Yes" below he has the option of selecting any of the six CheckBox below. If the user clicks on Radio Button...
6
by: NH | last post by:
I want to allow users to enter dates in a text box in either the US "MM dd yy" format or the UK "dd MM yy" format. But how can I validate these dates? All the date functions e.g. ISdate,...
6
by: Steve Ryan | last post by:
can anyone send me to some good examples of edit in place for data frid columns i see dot net let me assign a control in the IDE any good sources of reading on editing columns with controls...
1
by: SkipNRun | last post by:
I am a novice when comes to JavaScript, AJAX. I am working on a form, which will allow users to update their contact information. In order to make the form flexible, I need to use pull down list. ...
2
by: Mick Walker | last post by:
Hi, I have a problem that I have been trying to figure for a couple of days, and am wondering if anyone out there would be so kind as to give me a solution. (Deadline time) I am trying to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.