0

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!

3
Contributors
6
Replies
7
Views
8 Years
Discussion Span
Last Post by BzzBee
0

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 @#$%@#

0

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!

0

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

0

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!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.