TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-07-2008, 10:38 AM   #1 (permalink)
The Contributor
 
marxx's Avatar
 
Join Date: Sep 2007
Location: Finland
Posts: 45
Thanks: 3
marxx is on a distinguished road
Help 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!
Send a message via MSN to marxx
marxx is offline  
Reply With Quote
Old 06-07-2008, 12:11 PM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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';

Salathe is offline  
Reply With Quote
Old 06-07-2008, 05:58 PM   #3 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

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
delayedinsanity is offline  
Reply With Quote
Old 06-08-2008, 10:16 AM   #4 (permalink)
The Contributor
 
marxx's Avatar
 
Join Date: Sep 2007
Location: Finland
Posts: 45
Thanks: 3
marxx is on a distinguished road
Default

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.
Send a message via MSN to marxx
marxx is offline  
Reply With Quote
Old 06-08-2008, 10:36 AM   #5 (permalink)
The Contributor
 
Join Date: May 2008
Location: Denmark
Posts: 70
Thanks: 3
SpYkE112 is on a distinguished road
Default

well preg_ / ereg_ use the RegEx syntax, i think it is possible to google it, i don't understand it at all myself :P
SpYkE112 is offline  
Reply With Quote
Old 06-08-2008, 04:48 PM   #6 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

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
delayedinsanity is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 09:18 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design