Hi! I have a question that probably has a really simple answer :D. I have a regex that validates names:

elseif (!preg_match('#^[a-z0-9\' -]{1,35}$#i',$lastname
)) { include('includes/error.php'); }

Only if the name I enter has an apostrophe, it gives me my error message. Can someone please help me modify this regex so that it will allow a name like "O'Sullivan" or "Smith-Jacobs" to be entered? Thanks a ton!

Recommended Answers

All 6 Replies

Strange, I tested it and it works fine for me. O'Reilly matches, Smith matches, Smith-Jacobs matches. The only thing that doesn't match is stuff like @#$%@#

Thanks for the reply. I've tried it on two browsers now and can't get "O'Sullivan" to validate, while "Smith-Jacobs" does just fine!

Here's what I'm using:

<form action="" method="post">
<input type="text" name="text">
<input type="submit" name="submit">
</form>

<?php
if (isset($_POST['submit'])) {
   $text = $_POST['text'];
   if (!preg_match('#^[a-z0-9\' -]{1,35}$#i',$text
   )) { echo "fail"; }
   else { echo "success"; }
}
?>

Please help :). Much thanks!

The place to start debugging is by echoing the variable to see if it is what you think it is. You may have magicquotes turned on and O'Reilly is actually O\'Reilly

Thank you! And if it's echoing with an escaped apostrophe, how can I go about stopping that for validation? I made a work-around:

<form action="" method="post">
<input type="text" name="text">
<input type="submit" name="submit">
</form>

<?php
if (isset($_POST['submit'])) {
   $text = $_POST['text'];
   $text = str_replace("\\","",$text);
   if (!preg_match('#^[a-z0-9\' -]{1,35}$#i',$text
   )) { echo "fail"; }
   else { echo "success"; }
}
?>

But that one line would be a significant modification to a large-portion of code. Is there an easy way to go about this? Thanks again!

yes you can use stripslashes function, its good one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.