bobleny Posted June 14, 2006 Share Posted June 14, 2006 How do I check user input? If, say, some one types "c. :)". I want to be able to check for caricatures and require a certain length for the field. So, I need to be able to count the caricatures and check the length. How do I do that? Quote Link to comment https://forums.phpfreaks.com/topic/11943-checking-user-input/ Share on other sites More sharing options...
AndyB Posted June 14, 2006 Share Posted June 14, 2006 [a href=\"http://ca.php.net/manual/en/function.strlen.php\" target=\"_blank\"]strlen()[/a] Quote Link to comment https://forums.phpfreaks.com/topic/11943-checking-user-input/#findComment-45326 Share on other sites More sharing options...
bobleny Posted June 14, 2006 Author Share Posted June 14, 2006 So strlen() ill give me the number of characters. like$minname = strlen($_POST['name'])if ($minname => 3){ $good = TRUE}So that should work, I haven’t tested it. But then how do I say, for instance, name cannot contain spaces or @s or periods and what not? Quote Link to comment https://forums.phpfreaks.com/topic/11943-checking-user-input/#findComment-45342 Share on other sites More sharing options...
poirot Posted June 14, 2006 Share Posted June 14, 2006 [!--quoteo(post=383599:date=Jun 13 2006, 08:28 PM:name=Bob Leny)--][div class=\'quotetop\']QUOTE(Bob Leny @ Jun 13 2006, 08:28 PM) [snapback]383599[/snapback][/div][div class=\'quotemain\'][!--quotec--]So that should work, I haven’t tested it. But then how do I say, for instance, name cannot contain spaces or @s or periods and what not?[/quote]Regular Expressions.[a href=\"http://www.php.net/pcre\" target=\"_blank\"]http://www.php.net/pcre[/a][code]if (preg_match("/[@.]/", $str) { // @ or . found}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/11943-checking-user-input/#findComment-45343 Share on other sites More sharing options...
bobleny Posted June 14, 2006 Author Share Posted June 14, 2006 I don't get what that is at all! I can't figuer that out. :( Is there any other way that could do this? Maybe one I can understand? Quote Link to comment https://forums.phpfreaks.com/topic/11943-checking-user-input/#findComment-45345 Share on other sites More sharing options...
Monkeymatt Posted June 14, 2006 Share Posted June 14, 2006 This will also check if those characters are in the string.[code]if (strpos('@', $str) !== false || strpos('.', $str) !== false || strpos(' ', $str) !== false) { // space, period, or @ found}[/code]Monkeymatt Quote Link to comment https://forums.phpfreaks.com/topic/11943-checking-user-input/#findComment-45348 Share on other sites More sharing options...
bobleny Posted June 14, 2006 Author Share Posted June 14, 2006 All right. That I understand! Thanks to all Quote Link to comment https://forums.phpfreaks.com/topic/11943-checking-user-input/#findComment-45355 Share on other sites More sharing options...
bobleny Posted June 14, 2006 Author Share Posted June 14, 2006 That will be good for sertant feilds but is there a previuslly made script or function or something that only allows letters and numbers?Actually I have this,[code] if (strpos('@', $_POST['forumsignupname']) !== false || strpos('.', $_POST['forumsignupname']) !== false || strpos(' ', $_POST['forumsignupname']) !== false || strpos('/', $_POST['forumsignupname']) !== false || strpos('\', $_POST['forumsignupname']) !== false || strpos('#', $_POST['forumsignupname']) !== false || strpos('!', $_POST['forumsignupname']) !== false || strpos('%', $_POST['forumsignupname']) !== false || strpos('^', $_POST['forumsignupname']) !== false || strpos('&', $_POST['forumsignupname']) !== false || strpos('*', $_POST['forumsignupname']) !== false || strpos('(', $_POST['forumsignupname']) !== false || strpos(')', $_POST['forumsignupname']) !== false || strpos('~', $_POST['forumsignupname']) !== false || strpos('`', $_POST['forumsignupname']) !== false || strpos('-', $_POST['forumsignupname']) !== false || strpos('+', $_POST['forumsignupname']) !== false || strpos('=', $_POST['forumsignupname']) !== false || strpos('?', $_POST['forumsignupname']) !== false || strpos('<', $_POST['forumsignupname']) !== false || strpos('>', $_POST['forumsignupname']) !== false || strpos(',', $_POST['forumsignupname']) !== false || strpos('{', $_POST['forumsignupname']) !== false || strpos('}', $_POST['forumsignupname']) !== false || strpos('[', $_POST['forumsignupname']) !== false || strpos(']', $_POST['forumsignupname']) !== false || strpos('|', $_POST['forumsignupname']) !== false || strpos(';', $_POST['forumsignupname']) !== false || strpos(':', $_POST['forumsignupname']) !== false || strpos('"', $_POST['forumsignupname']) !== false)[/code]But that wont work! I don't even have to try that to know it wont work! Im useing that coloring thing with PHP designer and half of its orange! lol so that will work for some stuff. Is there something I can use to only allow letters and numbers? Quote Link to comment https://forums.phpfreaks.com/topic/11943-checking-user-input/#findComment-45617 Share on other sites More sharing options...
poirot Posted June 14, 2006 Share Posted June 14, 2006 Do yourself a favor and use regex:[code]$name = trim($_POST['forumsignupname']);if (preg_match("/\W/", $name)) { // error, characters other than letters, numbers and the underscore "_" were found;}[/code]Because if you use your current code, besides the terrible ugliness, you will have to add each symbol you don't want manually. And there are lots of them. Quote Link to comment https://forums.phpfreaks.com/topic/11943-checking-user-input/#findComment-45645 Share on other sites More sharing options...
Koobi Posted June 14, 2006 Share Posted June 14, 2006 i would only use regexp if i'm trying to match a pattern and not something definite.for example, if you know that the user can only input the following words (perhaps he's limited to these choices because it's input via a select menu, ignoring the fact that forms can be spoofed):* VISA* Mastercard* American Expressnow since you know for sure that he will only enter those 3 words, it's ok to do an explicit check with an if condition.however, if your user is allowed to enter an email address, you would simply have to make sure the email conforms to the standard format of an email which is where regexp comes in because it does matching by a sequence of patterns. Apart from this being the most sensible way to do it, regexp is also fractionally slower than most other string functions because of it's nature.back to the previous case, if you know exactly what to expect, do an explicit string match. use [a href=\"http://www.php.net/ctype\" target=\"_blank\"]ctype functions[/a]. your version of PHP probably has this feature by default unless you were once neighbours with Fred and Wilma :) Quote Link to comment https://forums.phpfreaks.com/topic/11943-checking-user-input/#findComment-45658 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.