473,387 Members | 1,485 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

!ereg not catching all cases

40
I'm trying to validate some user entered text, and I'm using the code below:
Expand|Select|Wrap|Line Numbers
  1. $last_name = trim($_POST[‘last_name’]);
  2. if ( !ereg(“[A-Za-z’ -]{1,50}”,$last_name)
  3. {
  4. do stuff to require user to reenter last name;
  5. }
  6.  
Now if I enter the text :"John"..the text is accepted as it should be.
Also if I just enter symbols such as or numbers...it gives the appropriate error message.

The issue at hand is that if I were to enter something like "<john>" it also accepts the text. How can I fix this?
Jul 23 '08 #1
3 1209
pbmods
5,821 Expert 4TB
Heya, BOMEz.

You'll want to use boundary markers in your regular expression:
Expand|Select|Wrap|Line Numbers
  1. if ( !ereg("^[A-Za-z' -]{1,50}$",$last_name)
  2.  
You should also consider using preg_match() instead of ereg(), as it is more efficient (http://php.net/preg_match, http://php.net/ereg).
Jul 23 '08 #2
BOMEz
40
Thank you. I got it working

I also looked into using preg_match instead.
Jul 23 '08 #3
pbmods
5,821 Expert 4TB
Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime (:
Jul 24 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Stefan Gangefors | last post by:
I'm trying to figure out what I'm doing wrong when using ereg(). This is my regexp: ereg("^]$", "]"); and that does'n work, but this does: ereg("^$", "[");
9
by: Fiore Alessandro | last post by:
Hi all I've installed Apache 2.0.48 and PHP 4.3.4 on a workstation running Linux, RedHat 9.0. Let's consider the following piece of code: $patt = "{3}/{6}"; if...
3
by: Martin Lucas-Smith | last post by:
Is there some way of using ereg to detect when certain filename extensions are supplied and to return false if so, WITHOUT using the ! operator before ereg () ? I have an API that allows as an...
2
by: pomho | last post by:
Hi all, Even having read the PHP Manual, I can't understand the difference between the ereg function and the preg_match... Could anyone help ? thx in advance, --
3
by: Aphrael | last post by:
Hello all, last time, ereg specialists did help me, so I ask again: base pattern: some texte her result: the number alone So ? any idea ? Aphrael ;-)
3
by: Rafal Zak | last post by:
I have a little problem with ereg (eregi) in PHP - in some cases it behaves differently than I expect and I don`t know if my expectations are strange or there is any "syntax subtlety" I have...
3
by: Dynamo | last post by:
Hi, Thanks to all who replied to original posting.As a direct result of now gaining a better understanding of regular expressions, instead of trying to use other peoples scripts I am now trying to...
3
by: news | last post by:
I'm trying to make sure a form has only one or two digits in either of two fields. I looked at php.net and http://www.regular-expressions.info/reference.html and this is what I put together, but...
18
by: yawnmoth | last post by:
Say I have the following script: <? $string = 'test'; if (eregi("^+$",$string)) { echo 'matches!'; } else {
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.