?
Solved

Form text validation

Posted on 2007-10-21
9
Medium Priority
?
751 Views
Last Modified: 2013-12-13
Hi there,

Can you please help me with a php script to compare text entered into a web form to the current date? Date will only be year and month.

Any help would be greatly appreciated...
0
Comment
Question by:fincon
9 Comments
 
LVL 49

Expert Comment

by:Roonaan
ID: 20118265
What are the comparison requirements?
0
 

Author Comment

by:fincon
ID: 20118287
Hi Roonan...

I need a user to enter a number like 703MSZT010853 (this is a product serial number), the first three digits is the manufacturing date (1st = year, 2nd = month) and the item has a 3 year warranty. I need to check these numbers with the current date to validate warranty on the unit and return with a message.

Thanks
0
 

Author Comment

by:fincon
ID: 20118290
Sorry - The 2nd AND 3rd digit is the month..
0
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!

 
LVL 20

Assisted Solution

by:steelseth12
steelseth12 earned 800 total points
ID: 20119165
$str = "703MSZT010853";

$temp = "";

for($i=0;$i<strlen($str);$i++) {


      if(is_numeric($str[$i])) {
      
      
            $temp .=$str[$i];      
      
      }else{
      
            break;
            
      }
      
}

$temp =  strrev($temp);

$arr = str_split($temp,2);

$month = strrev($arr[0]);

$year = strrev($arr[1]);


$post_date = mktime(0,0,0,$month,1,$year+3);

$difference = intval(($post_date - time())/86400)/356;


if($difference > 0 ) {

      echo "Under Warranty";
      
}else{

      echo "Warranty Expired";

}


It may seem a bit long ... its because you represent year as 7 and not 07. This will also work when you get to 2010
0
 

Author Comment

by:fincon
ID: 20120644
steelseth,

Thank you for your input, however, I get the following error:

PHP Fatal error: Call to undefined function: str_split() in C:\Domains\testcrt.co.za\wwwroot\lgsn.php on line 24
0
 

Author Comment

by:fincon
ID: 20120846
Is there possibly a way of checking this in normal HTML?
0
 

Author Comment

by:fincon
ID: 20121684
Also, can it be validated in months instead of years?
0
 
LVL 11

Accepted Solution

by:
AlexanderR earned 1200 total points
ID: 20123388
steelseth12 has a good answer, however fincon unfortunatly is still in php4 where there is no str_split

This should work across all versions and it counts it in months
$str = "203MSZT010853";

//get todays date
$Today = mktime(0,0,0,date("m"),0,date("Y"));

//extract and computer manufacture's date
$ManufacturerYear = substr($str,0,1);
$ManufacturerMonth = substr($str,1,2);
$ManufacturerDate = mktime(0,0,0,$ManufacturerMonth,0,$ManufacturerYear);

//Check difference
//3 years = 36 months * 30 days * 24 hours * 60 min * 60 sec = 93312000 seconds
if(($Today-$ManufacturerDate)>93312000){
      echo "Warranty expired";
} else {
      echo "Under warranty";
}
0
 

Author Comment

by:fincon
ID: 20123661
Thank you guys! As alwasy, expert advice for a beginner like me! I appreciate all your time and effort! Thanks
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question