| | |
Validate Input Fields
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
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.
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!
Keep PEACE alive!
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:
php Syntax (Toggle Plain Text)
if (!ereg('^[0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]$', $date)){ //I got an error when I used "[0-9]{2}" instead of "[0-9][0-9]" //^ matches start of string and $ matches end. &errors_present = TRUE; $errors .= "date not valid"; } if (!ereg('^[a-zA-Z ]+$', $date)){ //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.
★ 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.
preg_match is usually faster.
should work, but its not tested.
PHP Syntax (Toggle Plain Text)
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.
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:
PHP Syntax (Toggle Plain Text)
function is_date($value) { // If the value of the field is empty, immediately return false. if (empty($value)) { return FALSE; } // Attach the exploded values to variables. list($day, $month, $year) = explode("/", $value); // Check those variables in the checkdate function. If it's valid, the function returns true. return checkdate($month, $day, $year); }
"Beneath this mask there is more than flesh. Beneath this mask there is an idea, Mr. Creedy, and ideas are bulletproof." - V
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?
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!
Keep PEACE alive!
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.
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.
•
•
•
•
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?
PHP Syntax (Toggle Plain Text)
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
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.
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.
•
•
•
•
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?
PHP Syntax (Toggle Plain Text)
$time = "0900 - 1730"; // If there is one occurence of the needle in the value, then explode the array. // If there is not, set it to an empty array. if (substr_count($time, " - ") == 1) { list($startTime, $endTime) = explode(" - ", $time); } else { list($startTime, $endTime) = array(0, 0); }
String:
PHP Syntax (Toggle Plain Text)
$name = O'Reilly $sanitisedName = mysql_real_escape_string($name);
"Beneath this mask there is more than flesh. Beneath this mask there is an idea, Mr. Creedy, and ideas are bulletproof." - V
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?
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!
Keep PEACE alive!
![]() |
Similar Threads
- how to validate text file, loading into listbox (Visual Basic 4 / 5 / 6)
- Homework Help (Java)
- Homework Help (Community Introductions)
- Javascript, Form fields validation and submit (JavaScript / DHTML / AJAX)
- "how to": 2 questions: (Java)
- Input Text colour in forms and submit (Graphics and Multimedia)
Other Threads in the PHP Forum
- Previous Thread: need help with two simple php scripts
- Next Thread: php validation before file upload
| Thread Tools | Search this Thread |
apache api array beginner binary body broken buttons cakephp checkbox class cms code cron curl database date date/time display dynamic ebooks echo email error file files folder form forms function functions global google href htaccess html image include insert ip javascript joomla limit link list login mail mediawiki menu mlm msqli_multi_query multiple mycodeisbad mysql number oop parameter paypal pdf php phpincludeissue problem query radio random recourse recursion regex remote script search seo server sessions sms source sp space speed sql static subdomain syntax system table tag tutorial update upload url validator variable vbulletin video web webdesign white wordpress xml youtube






