TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   User input can contains? (http://www.talkphp.com/absolute-beginners/2919-user-input-can-contains.html)

marxx 06-07-2008 10:38 AM

User input can contains?
 
If user input can contains numbers, slashes (-) and plus sign but nothing else, how do I check that?

Is it ereg function? Just can't figure out those patterns and such.. =)



Thanks for all help!

Salathe 06-07-2008 12:11 PM

You could look to use strspn or strcspn rather than using the more 'powerful' (for want of a better term) regular expression functions.

PHP Code:

$test '+44-1234-5678';

if (
strcspn($test'1234567890+-'))
{
    echo 
'Test contains one or more invalid characters';
}
else
{
    echo 
'Test is nice and only contains digits, plus or dash';



delayedinsanity 06-07-2008 05:58 PM

There's also the ctype functions to keep in mind for other basic checks. If you wanted a regular expression to do this, it would look something like,

PHP Code:

$szString "1234567890+-";

if (
preg_match("/^[0-9-+]+$/"$szString)) {
    echo 
"String '{$szString}' contains only valid characters";
} else {
    echo 
"String '{$szString}' contains invalid characters";


I was trying to come up with a decent one that didn't use the start of string/end of string qualifiers, just to see if I could, and... nope.
-m

marxx 06-08-2008 10:16 AM

Thanks for answering!

@Salathe
Those functions were new to me and thanks for pointing them. Unfortunately your example didn't work in my case.

ie. If I want to user to do phone number and nothing else.
Code:

+358-50-1112233 = passed
050-1112233 = passed
0501112233 = passed
aa0501112233 = failed as it should
05011122aa33 = should failed but script passed it.

So that didn't work.

Otherhand, delayedinsanity your example is what I was looking for!! =)
Code:

If there is something else that 1234567890+- then fail!!
Thanks guys!

btw. Does someone know is there some simple instructions how to use those patterns?
Thanks.

SpYkE112 06-08-2008 10:36 AM

well preg_ / ereg_ use the RegEx syntax, i think it is possible to google it, i don't understand it at all myself :P

delayedinsanity 06-08-2008 04:48 PM

Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp Patterns
Regular Expressions Cheat Sheet - Cheat Sheets - ILoveJackDaniels.com

Those are a couple of my favourite's. Google 'regular expressions' to find more... the cheat sheet there is as simple as it gets, though might I suggest if you want to get a good handle on how it's working, you may want to find some example patterns and break them down to learn how they're matching what they're matching and why. Then create some random practice strings of your own and try to match various parts of them.
-m


All times are GMT. The time now is 05:39 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0