Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

preg_match question

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

preg_match question

Postby Rdubya » Tue Oct 02, 2012 6:14 pm

I am trying to use preg_match to make it so a user cannot enter numbers on the city entry.

What i have been using for most of my checks is:

if (preg_match('/^[[:alnum:]\ \'\-]{1,60}$/', stripslashes(trim($_POST['city']))) )

-----------

that line works, but it doesn't make it letters only.

I tried:

if (preg_match('/^[[a-zA-Z]\ \'\-]{1,60}$/', stripslashes(trim($_POST['city']))) )

That doesn't work...

hoping it is something small i am missing---

thanks for your help!!

-Ryan
Rdubya
New php-forum User
New php-forum User
 
Posts: 4
Joined: Sat Sep 22, 2012 2:30 pm

Re: preg_match question

Postby kyle04 » Wed Oct 03, 2012 3:12 pm

Code: Select all
if(
  preg_match('/[^a-zA-Z]/', stripslashes(trim($_POST['city'])))
){
// nasty characters
kyle04
New php-forum User
New php-forum User
 
Posts: 94
Joined: Sat Jul 07, 2012 1:36 pm

Re: preg_match question

Postby Rdubya » Thu Oct 04, 2012 2:35 pm

hi Kyle,

Thanks for the help.

When I tested to see what it would allow me to enter, it would still allow me to enter "test1". If I start the string with a number it won't work, but it seems like I can inject a number after a letter and it will work.

Any fix for that?

Thanks!
Ryan
Rdubya
New php-forum User
New php-forum User
 
Posts: 4
Joined: Sat Sep 22, 2012 2:30 pm

Re: preg_match question

Postby seandisanti » Thu Oct 04, 2012 3:30 pm

Code: Select all
<?php
$city = "1test1";
$pattern = '[\d]';
$city = preg_replace($pattern,'',$city);
echo $city;
?>
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 11 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron