Trium918 Posted April 24, 2007 Share Posted April 24, 2007 Problem: write a script that will only allow the user to enter ,a-z A-Z 0-9, letters and numbers. I have another script that I am using to validate an email address and it's working using the same method. Unfortunately, this one isn't working. Note: Just Ideas <?php //clean out any SQL injections, but limited to numbers and letters only if (!valid_entry($_POST['user_name'])) { do_html_header("Problem:"); echo "<p class=\"genmed\">That is not valid a user name. Please go back " ." and try again.</p>"; do_html_footer(); exit; } ?> <?php function valid_entry($user_name) { if(ereg("/[^a-zA-Z0-9]/",$user_name)) return true; else return false; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/48529-help-with-function/ Share on other sites More sharing options...
MadTechie Posted April 24, 2007 Share Posted April 24, 2007 <?php function valid_entry($user_name) { if(ereg("/[^a-zA-Z0-9]/",$user_name)) { return true; }else{ return false; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/48529-help-with-function/#findComment-237478 Share on other sites More sharing options...
Trium918 Posted April 24, 2007 Author Share Posted April 24, 2007 I got it working like this, but is it cool to do it this way? <?php function valid_entry($user_name) { if(ereg("^[a-zA-Z0-9]+$",$user_name)) return true; else return false; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/48529-help-with-function/#findComment-237490 Share on other sites More sharing options...
genericnumber1 Posted April 24, 2007 Share Posted April 24, 2007 why not... <?php function valid_entry($user_name) { return ereg("^[a-zA-Z0-9]+$",$user_name); } ?> but yes, you can do it like that, but most people use brackets to keep it easy to read Quote Link to comment https://forums.phpfreaks.com/topic/48529-help-with-function/#findComment-237494 Share on other sites More sharing options...
Trium918 Posted April 24, 2007 Author Share Posted April 24, 2007 why not... <?php function valid_entry($user_name) { return ereg("^[a-zA-Z0-9]+$",$user_name); } ?> but yes, you can do it like that, but most people use brackets to keep it easy to read Thanks Is it possible to set an array for every field that will be using numbers and letters and place that variable where $user_name is and would I have to have a loop? Quote Link to comment https://forums.phpfreaks.com/topic/48529-help-with-function/#findComment-237501 Share on other sites More sharing options...
MadTechie Posted April 24, 2007 Share Posted April 24, 2007 it would be messy on the return as it will return an array of booleans you can create a loop and just all the function Quote Link to comment https://forums.phpfreaks.com/topic/48529-help-with-function/#findComment-237505 Share on other sites More sharing options...
Trium918 Posted April 24, 2007 Author Share Posted April 24, 2007 I donnot want to have to create this for every form field. <?php // clean out any SQL injections, but limited to numbers and letters only if (!valid_entry($_POST['address'])) { do_html_header("Problem:"); echo "<p class=\"genmed\">That is not valid a user name. Please go back " ." and try again.</p>"; do_html_footer(); exit; } // clean out any SQL injections, but limited to numbers and letters only if (!valid_entry($_POST['lastname'])) { do_html_header("Problem:"); echo "<p class=\"genmed\">That is not valid a user name. Please go back " ." and try again.</p>"; do_html_footer(); exit; } // clean out any SQL injections, but limited to numbers and letters only if (!valid_entry($_POST['firstname'])) { do_html_header("Problem:"); echo "<p class=\"genmed\">That is not valid a user name. Please go back " ." and try again.</p>"; do_html_footer(); exit; } ?> <?php function valid_entry($user_name) { if(ereg("^[a-zA-Z0-9]+$",$user_name)) return true; else return false; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/48529-help-with-function/#findComment-237517 Share on other sites More sharing options...
MadTechie Posted April 24, 2007 Share Posted April 24, 2007 you could <?php foreach($_POST as $P) { if (!valid_entry($P)) { do_html_header("Problem:"); echo "<p class=\"genmed\">That is not valid data. Please go back " ." and try again.</p>"; do_html_footer(); exit; } } ?> the address would fail Quote Link to comment https://forums.phpfreaks.com/topic/48529-help-with-function/#findComment-237523 Share on other sites More sharing options...
Trium918 Posted April 24, 2007 Author Share Posted April 24, 2007 you could <?php foreach($_POST as $P) { if (!valid_entry($P)) { do_html_header("Problem:"); echo "<p class=\"genmed\">That is not valid data. Please go back " ." and try again.</p>"; do_html_footer(); exit; } } ?> the address would fail I am getting the echo Quote Link to comment https://forums.phpfreaks.com/topic/48529-help-with-function/#findComment-237539 Share on other sites More sharing options...
genericnumber1 Posted April 25, 2007 Share Posted April 25, 2007 <?php function valid_entry($entries) { $valid = true; foreach((array)$entries as $entry) { if(!ereg("^[a-zA-Z0-9]+$", $entry)) { $valid = false; } } return $valid; } $entries = array($_POST['username'], $_POST['password']); if(!valid_entry($entries)) { echo 'Bad entry!'; } ?> you could pass that function a string or an array. Quote Link to comment https://forums.phpfreaks.com/topic/48529-help-with-function/#findComment-237568 Share on other sites More sharing options...
Trium918 Posted April 25, 2007 Author Share Posted April 25, 2007 <?php function valid_entry($entries) { $valid = true; foreach((array)$entries as $entry) { if(!ereg("^[a-zA-Z0-9]+$", $entry)) { $valid = false; } } return $valid; } $entries = array($_POST['username'], $_POST['password']); if(!valid_entry($entries)) { echo 'Bad entry!'; } ?> you could pass that function a string or an array. It is giving me the echo 'bad entry'; even when there is only letters and numbers. Quote Link to comment https://forums.phpfreaks.com/topic/48529-help-with-function/#findComment-237616 Share on other sites More sharing options...
otuatail Posted April 25, 2007 Share Posted April 25, 2007 Sorry to be a snake in the grass, but been there got a t-shirt. What is a valid email address. @aol.com @biz.co.uk endless ? my atempt was it had to have one and only one @ only one dot after and have 1 or more charecters before the @ emails I think have to start with a letter a - z True email validation is very hard Quote Link to comment https://forums.phpfreaks.com/topic/48529-help-with-function/#findComment-237626 Share on other sites More sharing options...
MadTechie Posted April 25, 2007 Share Posted April 25, 2007 for email you can try <?php if (eregi('\\A\\b[A-Z0-9._%-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b\\z', $email)) { echo "valid"; } ?> this is untested Quote Link to comment https://forums.phpfreaks.com/topic/48529-help-with-function/#findComment-237976 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.