Jump to content

Archived

This topic is now archived and is closed to further replies.

bobleny

Checking user input.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
[a href=\"http://ca.php.net/manual/en/function.strlen.php\" target=\"_blank\"]strlen()[/a]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
[!--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]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.