Validate Input Fields

Thread Solved

Join Date: Oct 2005
Posts: 93
Reputation: humbug is an unknown quantity at this point 
Solved Threads: 13
humbug's Avatar
humbug humbug is offline Offline
Junior Poster in Training

Re: Validate Input Fields

 
1
  #11
Nov 2nd, 2008
Originally Posted by antwan1986 View Post
Perhaps I've misunderstood the question, and the two previous posters used regular expressions but I would normally test a date with something like the following:

  1. function is_date($value) {
  2. // If the value of the field is empty, immediately return false.
  3. if (empty($value)) { return FALSE; }
  4.  
  5. // Attach the exploded values to variables.
  6. list($day, $month, $year) = explode("/", $value);
  7.  
  8. // Check those variables in the checkdate function. If it's valid, the function returns true.
  9. return checkdate($month, $day, $year);
  10. }
This is a great method for the date. If you wanted to you could even return "$year - $month - $day"; if it is a valid date or FALSE if it isn't seeing as you already have the variables set.

As for the time, I would probably take the input as a string and use one of the regular expression checks above to see if it consists of 4 numbers, then typecast it to an integer and check if ($time_num >= 900 && $time_num <= 1700) { echo "time is valid"; }
"If your not having fun, your doing something wrong." - Humbug
★ Did I help you out? Did I piss you off? Add to my reputation!
The Gabriel Method is a great book for losing weight and keeping healthy - I know Jon Gabriel Personally.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 50
Reputation: jackakos is an unknown quantity at this point 
Solved Threads: 0
jackakos's Avatar
jackakos jackakos is offline Offline
Junior Poster in Training

Re: Validate Input Fields

 
0
  #12
Nov 5th, 2008
Great Stuff!

Your postings have giving me much understanding of most issues.

start_time is a varchar data type, is it possible to perform an addition on this variable?

Supposing $start_time has a value 1100 because I am using 24 hour format can I add a value to it?

ie. $start_time = $start_time + 100;
It is NO SHAME to ask when you want to LEARN, but FOOLISH to behave knowledgeable when YOU DON'T.
Keep PEACE alive!
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 93
Reputation: humbug is an unknown quantity at this point 
Solved Threads: 13
humbug's Avatar
humbug humbug is offline Offline
Junior Poster in Training

Re: Validate Input Fields

 
0
  #13
Nov 6th, 2008
In php, if you try to perform an operation on $start_time (assuming that it is a variable in the script containing a string) it will be used as an integer. It works fine if the string is something like "1234". $start_time will then be converted to an integer variable (because it is on the left of the "=").

If there is a column in a mysql table that is named "start_time" and is a varchar datatype and you do something like $start_time = $row['start_time'] (where $row contains a row of a result from a query) then $start_time will be a string.

If you are writing a query to submit data to the mysql database and you can do something like
  1. $query = "INSERT INTO table (`start_time`, `finish_time`) VALUES ( " . $start_time . ", " . $finish_time . ")";
  2. //or
  3. $query = "INSERT INTO table (`start_time`, `finish_time`) VALUES ( '" . $start_time . "', '" . $finish_time . "')";
With the single quotes ('), the mysql treats whatever is in them as a string no matter what. When you do $string = "$int"; or $string = "number is " . $int; , $int is used as a string. So when you are creating the query it doesn't matter what type $start_time is. Without the single quotes I think SQL will assume it's whatever type it needs to be. (mysql converts it to the right data type anyway so there's usually no problem). Just be careful doing "... WHERE culumn_name=$string" instead of "... WHERE culumn_name='$string'" because other wise only the first word of $string will be used.

This changing types is called "typecasting" if you want to research it more or anything like that.
"If your not having fun, your doing something wrong." - Humbug
★ Did I help you out? Did I piss you off? Add to my reputation!
The Gabriel Method is a great book for losing weight and keeping healthy - I know Jon Gabriel Personally.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 50
Reputation: jackakos is an unknown quantity at this point 
Solved Threads: 0
jackakos's Avatar
jackakos jackakos is offline Offline
Junior Poster in Training

Re: Validate Input Fields

 
0
  #14
Nov 6th, 2008
Great stuff and thanks to everyone who shared an idea.

Thanx
It is NO SHAME to ask when you want to LEARN, but FOOLISH to behave knowledgeable when YOU DON'T.
Keep PEACE alive!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC