Validate Input Fields

Thread Solved

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

Validate Input Fields

 
0
  #1
Nov 1st, 2008
Hi Folks, is there a way to validate input fields using only PHP scripts before inserting into a MySQL database?

I have fileds for:
Date (DD-MM-YYYY)
Time (24 hour format between 0900 and 1700)
Location (String)

Your suggestions will be a boost.
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
  #2
Nov 1st, 2008
I have just written a script to do just that. I used ereg() to do a search for a regular expression that will verify a valid entry. e.g:
  1. if (!ereg('^[0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]$', $date)){
  2. //I got an error when I used "[0-9]{2}" instead of "[0-9][0-9]"
  3. //^ matches start of string and $ matches end.
  4. &errors_present = TRUE;
  5. $errors .= "date not valid";
  6. }
  7. if (!ereg('^[a-zA-Z ]+$', $date)){
  8. //etc.
"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: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: Validate Input Fields

 
0
  #3
Nov 1st, 2008
preg_match is usually faster.

  1. preg_match("/^[0-9]{2}-[0-9]{2}-[0-9]{4}$/",$date)

should work, but its not tested.
Last edited by kkeith29; Nov 1st, 2008 at 9:36 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 110
Reputation: antwan1986 is an unknown quantity at this point 
Solved Threads: 8
antwan1986's Avatar
antwan1986 antwan1986 is offline Offline
Junior Poster

Re: Validate Input Fields

 
1
  #4
Nov 1st, 2008
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. }
"Beneath this mask there is more than flesh. Beneath this mask there is an idea, Mr. Creedy, and ideas are bulletproof." - V
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
  #5
Nov 1st, 2008
I am going to tryyour solutions with the date - wht about the 24 hour time format with the respective time given:

0900 - 1700

and Strings to ensure they are not used as SQL injection by users?
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: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: Validate Input Fields

 
0
  #6
Nov 1st, 2008
the time format could use a regex or a combination of is_numeric and strlen. and for the strings just run mysql_real_escape_string and you should be good.

there a many solutions to this problem so choose the one that will work for you best and run with it.
Last edited by kkeith29; Nov 1st, 2008 at 10:11 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 110
Reputation: antwan1986 is an unknown quantity at this point 
Solved Threads: 8
antwan1986's Avatar
antwan1986 antwan1986 is offline Offline
Junior Poster

Re: Validate Input Fields

 
0
  #7
Nov 1st, 2008
Originally Posted by jackakos View Post
I am going to tryyour solutions with the date - wht about the 24 hour time format with the respective time given:

0900 - 1700

and Strings to ensure they are not used as SQL injection by users?

Something like this perhaps?

  1. preg_match("/^[0-9]{4} - [0-9]{4}$/", $time)
"Beneath this mask there is more than flesh. Beneath this mask there is an idea, Mr. Creedy, and ideas are bulletproof." - V
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,402
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 224
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: Validate Input Fields

 
1
  #8
Nov 1st, 2008
Just in case you hadn't fixed it yet, (\d{2}\-){2}\d{4} is the correct regular expression. for 12-34-5678 format. To correctly write a regular expression you have to know exactly what your input must look like. If you post what you want in your fields it would be easier.
Last edited by ShawnCplus; Nov 1st, 2008 at 10:12 pm.
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 110
Reputation: antwan1986 is an unknown quantity at this point 
Solved Threads: 8
antwan1986's Avatar
antwan1986 antwan1986 is offline Offline
Junior Poster

Re: Validate Input Fields

 
0
  #9
Nov 1st, 2008
Originally Posted by jackakos View Post
I am going to tryyour solutions with the date - wht about the 24 hour time format with the respective time given:

0900 - 1700

and Strings to ensure they are not used as SQL injection by users?
Time:
  1. $time = "0900 - 1730";
  2.  
  3. // If there is one occurence of the needle in the value, then explode the array.
  4. // If there is not, set it to an empty array.
  5. if (substr_count($time, " - ") == 1) {
  6. list($startTime, $endTime) = explode(" - ", $time);
  7. } else {
  8. list($startTime, $endTime) = array(0, 0);
  9. }

String:

  1. $name = O'Reilly
  2.  
  3. $sanitisedName = mysql_real_escape_string($name);
  4.  
"Beneath this mask there is more than flesh. Beneath this mask there is an idea, Mr. Creedy, and ideas are bulletproof." - V
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
  #10
Nov 2nd, 2008
Hmm, your inputs are great.

About the time, I do not want a user to type any characters less than 4
ie '0900'
and any value less than 0900 or greater than 1700 should be rejected

With the date, I am accepting it as dd-mm-yyyy, I read some where that MySQL easily handles date as yyyy-mm-dd
in other to verify that, months are accepted as 01 to 12 and days do not go beyond 31 how would this be done?
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