Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
This week, we're updating our course layouts and descriptions. Presentation and materials always gently change over time, but just occasionally there's a need to make a step change to clear out some of the old and roll in the new. That's now happening - but over a long and complex site it's not instant and you'll see sections of the site changing up to and including 19th September.

See also [here] for status update
 
Regular Expression Problem

Posted by keith (keith), 1 March 2005
Hi,

I have a problem with this regular expression script and cannot figure out why it doesn't work.

Code:
if (ereg("([0-9 \(\)\+]*)",$_POST['fax'])){
$valid_fax=" Fax Number :  ";
}else{
$valid_fax="<Font Color='#AA0000'> Fax Number :  </FONT>";
$valid_fail=1;
}


The code is supposed to just check that only numbers, brackets'()', spaces or the plus'+' sign are allowed.
eg +44 (0) 118 9123456

However it seems to allow anything!

If I change the expression to "([0-9 \(\)\+]+)". Then it works (but at least one character must be given).
I want the fax number to be optional but validated if entered.

What's wrong with this?
I put it into both 'Regex Coach' and 'Regular Expression Editor' software and both seem to think it's fine.

Why is the change from '+' to '*' causing an issue?

Posted by admin (Graham Ellis), 1 March 2005
"+" means ONE or more occurrence of the element preceeding, and "* means ZERO or more occurrences.   If your entire regular expression has an * after it, then it will always match as every possible input string contains nothing.   That's a tough one to work even in English.

You probably want to use anchors (assertions) to say that your regular expression starts with ... and ends with ... rather that the default which is "contains".   Try:
ereg('^[0-9 \(\)\+]*$',$_POST['fax'])

Posted by keith (keith), 1 March 2005
That's cracked it.

Thanks Graham  



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho