eregi used to be so easy, but I’m still struggling to convert to preg_match. Just don’t seem to get it.
On a contact form, I’m trying to make sure that the Name field only contains letters, a hyphen (-), apostrophes (') and spaces. This is what I have, but it is letting all sorts of characters through, such as [.
Any suggestions as to what I’m doing wrong?
This is what I have at this point:
EDIT: OK, it seems to work OK if I use this:
But I was under the impression that A-z was a valid shorthand for A-Za-z, but it seems to have been causing a problem here. Is there something else I’m missing?
You’ll have to use the
i modifier to make it case-insensitive.
Thanks Anthony. That works nicely too. (A while back, Remon suggested I start and end with the ~ as well, but I forgot. )
While you’re here, I saw this as part of a preg_match for an email address, but I can’t work out what it means. Does it mean anything to you? And does it have any use?
Sorry Ralph, that means nothing to me. It actually looks like it just matches “n/a” or “N/A”.
O dear, it does indeed. I now realize I wrote that myself for a special situation where the user could enter an email address or indicate N/A. I must be getting old.
Thanks again for your help.