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
Python, Lua and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, C, C++, Ruby, MySQL and Linux / Tomcat systems
Regular Expression Problem

Posted by keith (keith), 1 March 2005

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

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

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 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., 2018: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01225 708225 • FAX: 01225 793803 • EMAIL: • WEB: • SKYPE: wellho