Link to home
Start Free TrialLog in
Avatar of Happylad
Happylad

asked on

Compare a text field in an HTML Form

I have created a form on our site. One of the field is a date. I need to be able to compare the date with today's date. If the date entered is equal to today, then display an error message when trying to submit the form. How can I do that?
Thanks
Avatar of Steve Bink
Steve Bink
Flag of United States of America image

This link has a whole bunch of tutorials and example for this sort of thing:

http://www.google.com/search?q=html+form+date+checking

Look over the basics, and try a couple out.  If you run into problems, post back and we'll sort them out.
Avatar of dragoncc
dragoncc

Since you didn't provide your own vars I just made some up.

$origdate = $_POST['orig_date'];
$today = date("Y-m-d"); // format - 2007-10-16

if($origdate == $today){

echo "error";

} else {

// something else

 }
Using Javascript:
But, you will probably find the tools you need to parse, manipulate, and
compare date objects at
http://www.JavascriptToolbox.com/lib/date/

----------------------------------------------------------------------------
seen in
news:comp.lang.javascript, joeyej <joseph.jasinski@quinnipiac.edu>
posted :[color=blue]
>How do I compare today's date with this string (in my inc file) so that
>I can set an alert if date choice i.e. May 15, 2006 not at least
>greater than two days from current date?
>
><option value="May 15, 2006, (Monday), 10am">May 15, 2006, (Monday),
>10am[/color]

If you had used the newsgroup FAQ before posting (see sig below), you
might not have needed to ask.

Change the value format so that it is acceptable as a parameter to new
Date() in all target browsers. ISTM that it may be enough to add the
minutes - 10:00am - but it would be more sensible to use 2006/05/15
10:00 or 10:00:00. Using the 12-hour clock within data processing is
utter folly.

Preferably, edit the change into the value as it will be when the user
is about to select it; or edit it after it has been read, for example
with
S = S.replace(/(\d+)(a|p)/, "$1:00$2")


Then just do DS = new Date(S) and compare D with a Date Object DT
holding the desired date : OK = DS > DT .

You'll have to decide whether two days ahead includes the time component
or starts at midnight, and you'll need to think about Summer Time.

You could, alternatively, with the value string in ISO/FIPS form.
generate the date-ahead in matching form and do a string comparison.


You can also use something like this
<script type="text/javascript">
var myDate=new Date()

var today = document.getElementByID('your field').value;
if (myDate=today)
  alert("Sorry you got it wrong");
</script>
Avatar of Happylad

ASKER

I have another script that is used to enter the date in the text field of the form. I am not an expert with all that, and new to Javascript, so I would like to have the script run when I submit the form.

ErikTsomik, I like yours, very simple and looks like it can do what I want, but when I added it, it seems to ignore the script. Where is it supposed to be placed?

Thanks.
try something like this
<script type="text/javascript">
function isValiddate () {
var myDate=new Date()

var today = document.getElementByID('your field').value;
if (myDate=today)
  alert("Sorry you got it wrong");
}
</script>
And then call it  onsubmit="isValiddate ()"
Why not just make the text field disabled so users can't change it, then place todays date in there like this.

<input type="text" name="unused" size="15" value="<?=date("m-d-y")?>" disabled="disabled" />
<input type="hidden" name="todaysDate" value="<?=date("m-d-y")?>" />

then it will always be todays date, and the users can't change it.
Because they pick a date from a calendar, then the date shows up in the textfield.
The reason I need this is because they cannot select today's date. If they do, I want a message box to shows up and tell them that they have to pick another date.
Sorry misread what you wanted.

<script language="javascript">
var today = new Date()

function validateDate(thisForm) {
   var myDate = thisForm.dateField.value;
   if (myDate == today) {
      alert("OOPS");
    }
}


In your form
<form name="myForm" ...... onSubmit="validateDate("myForm");">
<input type="text" name="dateField" />
</form>
> .. is equal to today
silly question: what is today?

For example if you (as human) are sitting in London/UK, using a browser through some kind of VPN on a system in Sydney/Australia and access a website on a server located in Hawai, what is "today" then?
<script language="javascript">
var today = new Date()


You can look at this site http://www.tizag.com/javascriptT/javascriptdate.php for date formating techniques.

If you have trouble, then post an example of how your date format looks when a client chooses it from the calendar popup.
My date looks like this once it is selected: 10/17/2007
Now this form, when submitted is mailed, could this have an impact on the java script? THere is also a first part of the form which is fowarded using PHP to the second form. Should I just put the date field into the first form?
Thanks
if you put 10/17/2007 this into javascript and try to compare it with javascript's date function, something like:
  var today = new Date()
then your comparsion will most likely fail.
Please read my comment http:#20091311 again and answer the question: what is "today" first.
The date that is selected is picked with another javascript ( there is a textbox, with an icon, you click on the icon, a calendar popup, the user select a date, then the date appear in the textbox with the format xx/xx/xxxx. So when I submit the form, I want to make sure that the date (which is the date on the west coast) is not equal to today's date.
DId I answer your question? Want to make sure I understood it. Thanks.
> DId I answer your question?
no
You still miss to explain "today".
It's silly but sounds very important to what you want to achive, or do I miss something (beside the definition of "today":)?
Again: read http:#20091311 
sorry, been away, I will answer today.
ASKER CERTIFIED SOLUTION
Avatar of nplib
nplib
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The problem I have now, is that the date being correct or not, the form still move on to be mailed. How do I stop the form to continue if the date is not correct?
Thanks.
in your html form

<form name="something" action="something.php" method="post" onSubmit="return validateDate(this);">

you could also use method="get" if you want.