Jump to content


Photo

Checking user input.


  • Please log in to reply
9 replies to this topic

#1 bobleny

bobleny
  • Members
  • PipPipPip
  • Advanced Member
  • 429 posts

Posted 14 June 2006 - 01:47 AM

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?
-- www.firemelt.net --
First do me a favor and read this: JavaScript is NOT Java - Then read this: www.php.net - When your done with that, read this Topic
After that, floors open. I and anyone else will be MORE than happy to answer YOUR query! [Topic Solved]
Cheer up, the worst has yet to come...

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 14 June 2006 - 01:55 AM

[a href=\"http://ca.php.net/manual/en/function.strlen.php\" target=\"_blank\"]strlen()[/a]
Legend has it that reading the manual never killed anyone.
My site

#3 bobleny

bobleny
  • Members
  • PipPipPip
  • Advanced Member
  • 429 posts

Posted 14 June 2006 - 03:28 AM

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?
-- www.firemelt.net --
First do me a favor and read this: JavaScript is NOT Java - Then read this: www.php.net - When your done with that, read this Topic
After that, floors open. I and anyone else will be MORE than happy to answer YOUR query! [Topic Solved]
Cheer up, the worst has yet to come...

#4 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 14 June 2006 - 03:31 AM

[!--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) View Post[/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]

if (preg_match("/[@.]/", $str) {
   // @ or . found
}

~ D Kuang

#5 bobleny

bobleny
  • Members
  • PipPipPip
  • Advanced Member
  • 429 posts

Posted 14 June 2006 - 03:46 AM

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?
-- www.firemelt.net --
First do me a favor and read this: JavaScript is NOT Java - Then read this: www.php.net - When your done with that, read this Topic
After that, floors open. I and anyone else will be MORE than happy to answer YOUR query! [Topic Solved]
Cheer up, the worst has yet to come...

#6 Monkeymatt

Monkeymatt
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts

Posted 14 June 2006 - 04:02 AM

This will also check if those characters are in the string.

if (strpos('@', $str) !== false || strpos('.', $str) !== false ||  strpos(' ', $str) !== false) {
   // space, period, or @ found
}

Monkeymatt

#7 bobleny

bobleny
  • Members
  • PipPipPip
  • Advanced Member
  • 429 posts

Posted 14 June 2006 - 04:23 AM

All right. That I understand! Thanks to all
-- www.firemelt.net --
First do me a favor and read this: JavaScript is NOT Java - Then read this: www.php.net - When your done with that, read this Topic
After that, floors open. I and anyone else will be MORE than happy to answer YOUR query! [Topic Solved]
Cheer up, the worst has yet to come...

#8 bobleny

bobleny
  • Members
  • PipPipPip
  • Advanced Member
  • 429 posts

Posted 14 June 2006 - 04:56 PM

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,

    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)

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?
-- www.firemelt.net --
First do me a favor and read this: JavaScript is NOT Java - Then read this: www.php.net - When your done with that, read this Topic
After that, floors open. I and anyone else will be MORE than happy to answer YOUR query! [Topic Solved]
Cheer up, the worst has yet to come...

#9 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 14 June 2006 - 05:40 PM

Do yourself a favor and use regex:

$name = trim($_POST['forumsignupname']);
if (preg_match("/\W/", $name)) {
   // error, characters other than letters, numbers and the underscore "_" were found;
}

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.
~ D Kuang

#10 Koobi

Koobi
  • Staff Alumni
  • Advanced Member
  • 419 posts
  • LocationColombo, Sri Lanka | South Asia

Posted 14 June 2006 - 06:14 PM

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 Express

now 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 :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users