preg_match validation

Reply

Join Date: Oct 2009
Posts: 12
Reputation: rokape is an unknown quantity at this point 
Solved Threads: 1
rokape rokape is offline Offline
Newbie Poster

preg_match validation

 
0
  #1
30 Days Ago
Hi,

I have a piece of code below:

  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
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 524
Reputation: Will Gresham is on a distinguished road 
Solved Threads: 86
Sponsor
Will Gresham's Avatar
Will Gresham Will Gresham is offline Offline
Posting Pro
 
1
  #2
30 Days Ago
If you want to use preg functions, then this should work to match strings containing alphanumeric characters and spaces:
  1. $pattern = '#^[a-z0-9\x20]+$#i';
  2. if (preg_match($pattern, $new_pagealias)) $error = "Invalid page alias. Use only normal characters.";
AJAX is not a programming language, scripting language or any other sort of language.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 12
Reputation: rokape is an unknown quantity at this point 
Solved Threads: 1
rokape rokape is offline Offline
Newbie Poster
 
0
  #3
23 Days Ago
Hi,

Thanks for your reply but it doesnt work. If I put home! through it it works fine and submits it to the database
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 524
Reputation: Will Gresham is on a distinguished road 
Solved Threads: 86
Sponsor
Will Gresham's Avatar
Will Gresham Will Gresham is offline Offline
Posting Pro
 
0
  #4
23 Days Ago
Post up the actual code you are using at the moment.
AJAX is not a programming language, scripting language or any other sort of language.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC