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
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho