Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 355
  • Last Modified:

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
0
Happylad
Asked:
Happylad
  • 6
  • 5
  • 3
  • +4
1 Solution
 
Steve BinkCommented:
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.
0
 
dragonccCommented:
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

 }
0
 
Bradley HaynesCommented:
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.


0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
erikTsomikSystem Architect, CF programmer Commented:
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>
0
 
HappyladAuthor Commented:
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.
0
 
erikTsomikSystem Architect, CF programmer Commented:
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 ()"
0
 
nplibCommented:
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.
0
 
HappyladAuthor Commented:
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.
0
 
nplibCommented:
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>
0
 
ahoffmannCommented:
> .. 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?
0
 
nplibCommented:
<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.
0
 
HappyladAuthor Commented:
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
0
 
ahoffmannCommented:
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.
0
 
HappyladAuthor Commented:
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.
0
 
ahoffmannCommented:
> 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 
0
 
nplibCommented:
sorry, been away, I will answer today.
0
 
nplibCommented:
Here you go

these functions will check todays date in the same format as your date and if it equals today, then it will present an error message.
The leadingZero() function will add a single leading zero to the date for numbers like 1,2,3,4... so they appear like 01, 02, 03, 04.....
today is the variable that contains todays date in your format.

      function leadingZero(x){
            return (x>9)?x:'0'+x;
      }


      var todayDay, todayMonth, todayYear, today
      var todayDate = new Date();
      todayDay = todayDate.getDate();
      todayMonth = todayDate.getMonth() + 1;
      todayYear = todayDate.getFullYear();
      todayDay = leadingZero(todayDay);
      todayMonth = leadingZero(todayMonth);

      today = todayMonth + "/" + todayDay + "/" + todayYear;

      function validateDate(thisForm) {
            var myDate = thisForm.dateField.value;
            if (myDate == today) {
                  alert("OOPS");
            }
      }
0
 
HappyladAuthor Commented:
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.
0
 
nplibCommented:
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.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 5
  • 3
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now