944,082 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 642
  • PHP RSS
Oct 30th, 2009
0

preg_match validation

Expand Post »
Hi,

I have a piece of code below:

php Syntax (Toggle Plain Text)
  1. // Check our data
  2. if (preg_match('/[^a-zA-Z0-9]/', $new_pagealias)) $error = "Invalid page alias. Use only normal characters.";
  3. if (preg_match('/[^a-zA-Z0-9]/', $new_pagekeywords)) $error = "Invalid page keywords. Use only normal characters.";
  4. if (!$new_pagealias) $error = "Page alias is missing";
  5. if (!$new_pagename) $error = "Page name is missing";
  6. if (!$new_pagetitle) $error = "Page title is missing";

This should check if $new_pagealias & $new_pagekeywords contain invalid characters, other than a-bA-B0-9 etc which works fine.

Well. The first one which checks the page alias works fine. The second one to check the keywords, doesn't. If I post a var to it containing the string '@' it will go through it and enter it into the database, same goes for the string "holiday, sunshine, ' " My HTML page will echo the meta etc ending with: holiday, sunshine, ' '/>

This ends the tag early which will error some browsers, plus I just want it to be perfect and not have to echo invalid keywords.

Plus I need the preg_match for $new_pagekeywords to validate and allow commas.

At the moment if I put the string "My house!" through alias & keywords, it will error for alias, but if I change alias to "house' and leave keywords as "My house!" it won't define $error & stop the SQL input.

Help?

Thanks in advance,
Josh
Reputation Points: 9
Solved Threads: 2
Light Poster
rokape is offline Offline
36 posts
since Oct 2009
Oct 30th, 2009
1
Re: preg_match validation
If you want to use preg functions, then this should work to match strings containing alphanumeric characters and spaces:
PHP Syntax (Toggle Plain Text)
  1. $pattern = '#^[a-z0-9\x20]+$#i';
  2. if (preg_match($pattern, $new_pagealias)) $error = "Invalid page alias. Use only normal characters.";
Reputation Points: 96
Solved Threads: 124
Master Poster
Will Gresham is offline Offline
728 posts
since May 2008
Nov 6th, 2009
0
Re: preg_match validation
Hi,

Thanks for your reply but it doesnt work. If I put home! through it it works fine and submits it to the database
Reputation Points: 9
Solved Threads: 2
Light Poster
rokape is offline Offline
36 posts
since Oct 2009
Nov 6th, 2009
0
Re: preg_match validation
Post up the actual code you are using at the moment.
Reputation Points: 96
Solved Threads: 124
Master Poster
Will Gresham is offline Offline
728 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: .png image files upload
Next Thread in PHP Forum Timeline: Please Help Me Fix My PHP Register Form





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC