Jump to content

preg_match problem


Oziam

Recommended Posts

When I use the following code to check for posted characters it allows the insertion of ( ) brackets to be posted.

 

if(!preg_match('/^([-_ a-z0-9]){2,50}$/i', $_POST['title']))

{

echo "<script>alert('Please enter a Title 2-50 alphanumeric characters,\\nspaces, hyphens and underscores also accepted!)</script>";

}

 

It should on allow 2-50 alphanumeric characters and spaces, hyphens and underscores!

 

What am I missing?

Link to comment
Share on other sites

I mean if use the form field title and enter for example "Titlename (part 1)"

it will skip the error, it will continue with the rest of the code and insert into database!

 

Its got me ####  :facewall:

 

### EDIT ###

Ok I found it! I was looking at the wrong script, in my other script it checks the title with

 

!preg_match('/^([\'-_ a-z0-9]){2,50}$/i', $_POST['title'])

 

notice the \' before the hyphen, this was to allow single quotes and this for some reason is causing the drama and allowing other characters to be included!

If I remove the \' it works fine!

 

Thanks for your help!

Link to comment
Share on other sites

I just ran the following, and with one $_POST var uncommented at a time, it does exactly what it should do.

 

<?php
$_POST['title'] = "Titlename part 1";
//$_POST['title'] = "Titlename (part 1)";
if( !preg_match('/^([-_ a-z0-9]){2,50}$/i', $_POST['title']) ) {
    echo "<script>alert('Please enter a Title 2-50 alphanumeric characters,\\nspaces, hyphens and underscores also accepted!')</script>";
    echo 'Bad characters';
} else {
    // DB insert here.
    echo 'No bad characters';
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.