Jump to content


Photo

Simple Question... I think [Solved]


  • Please log in to reply
11 replies to this topic

#1 Brentley_11

Brentley_11
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 02 October 2006 - 08:25 PM

I'm pretty new to PHP and I was wondering how I can verify that the data that was submited via POST from a form that it only contains letters, numbers, or the underscore charater.  I've tried a few things but sofar I haven't had any luck.

#2 Zane

Zane
  • Administrators
  • Advanced Member
  • 4,134 posts

Posted 02 October 2006 - 08:28 PM

if(!ereg("[A-Za-z0-9_-]*",$_POST['yourVar]))
  echo "Your POST var doesn't validate";

btn_donate_SM.gif Want to thank me? Contribute to my PayPal piggy-bank
 

172938.png

#3 Brentley_11

Brentley_11
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 02 October 2006 - 08:36 PM

Is there a chance that I have php configured wrong? I just tested what you said and it didn't work.

$test = array("Test","Test_11","Test.11","TEST","test","()Test");

foreach ($test  as $var){
	echo $var." - ";
	if(!ereg("[A-Za-z0-9_-]*",$var)) echo "Doesn't validate";
	else  echo "Does validate";
	echo "<br>";
}

This was the output:
Test - Does validate
Test_11 - Does validate
Test.11 - Does validate
TEST - Does validate
test - Does validate
()Test - Does validate

The third and last one shouldn't validate.

#4 Zane

Zane
  • Administrators
  • Advanced Member
  • 4,134 posts

Posted 02 October 2006 - 08:46 PM

my bad...
instead of
[A-Za-z0-9_-]*

use

[A-Za-z0-9_-]+

btn_donate_SM.gif Want to thank me? Contribute to my PayPal piggy-bank
 

172938.png

#5 Brentley_11

Brentley_11
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 02 October 2006 - 08:51 PM

Everything still validates.  ???

Code:
$test = array("Test","Test_11","Test.11","TEST","test","()Test");
echo "Test2 <br>";
foreach ($test  as $var){
	echo $var." - ";
	if(!ereg("[A-Za-z0-9_-]+",$var)) echo "Doesn't validate";
	else  echo "Does validate";
	echo "<br>";
}

Output:
Test2 
Test - Does validate
Test_11 - Does validate
Test.11 - Does validate
TEST - Does validate
test - Does validate
()Test - Does validate


#6 manmanman

manmanman
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 02 October 2006 - 08:52 PM

if(!ereg("[A-Za-z0-9_-]*",$_POST['yourVar]))
  echo "Your POST var doesn't validate";


Hmm... There is a missing quote on the end of yourVar, and also missing curly brackets. Also, I think you should use die() instead of echo.

<?php
if(!ereg("[A-Za-z0-9_-]*",$_POST['yourVar'])) {
  die("Your POST var doesn't validate");
}
?>


#7 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 02 October 2006 - 08:55 PM

And also a - is currently allowed,


<?php

function Check($string){
if(preg_match("/^[_a-zA-Z0-9]+$/", $string))
{
return true;
}
else
{
return false;
}
}


$test = array("Test","Test_11","Test.11","TEST","test","()Test","test-this");

foreach ($test  as $var){
	echo $var." - ";
	if(!Check($var)) echo "Doesn't validate";
	else  echo "Does validate";
	echo "<br>";
}

?>


#8 manmanman

manmanman
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 02 October 2006 - 08:57 PM

Is there a chance that I have php configured wrong? I just tested what you said and it didn't work.

$test = array("Test","Test_11","Test.11","TEST","test","()Test");

foreach ($test  as $var){
	echo $var." - ";
	if(!ereg("[A-Za-z0-9_-]*",$var)) echo "Doesn't validate";
	else  echo "Does validate";
	echo "<br>";
}

This was the output:
Test - Does validate
Test_11 - Does validate
Test.11 - Does validate
TEST - Does validate
test - Does validate
()Test - Does validate

The third and last one shouldn't validate.


Ok, reviewing this code, you have missing curly brackets.
Also, doing what alpnie did, removing the -, your code should be like this:

<?php
$test = array("Test","Test_11","Test.11","TEST","test","()Test");

foreach ($test as $var){
	echo $var." - ";
	if(!ereg("[_A-Za-z0-9]*",$var)) {
             echo "Doesn't validate";
	} else {  
             echo "Does validate"; 
        }
	echo "<br>";
}
?>


#9 Zane

Zane
  • Administrators
  • Advanced Member
  • 4,134 posts

Posted 02 October 2006 - 08:58 PM

this should do it
if(ereg("[^A-Za-z0-9_-]+", $var)

been out of practice of regex for a while

Ok, reviewing this code, you have missing curly brackets.


notice his IF statements only have one statement...
When your IF statements only do one thing...you don't need curly braces

btn_donate_SM.gif Want to thank me? Contribute to my PayPal piggy-bank
 

172938.png

#10 manmanman

manmanman
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 02 October 2006 - 09:03 PM

Try this; it works on my server:

<?php
$test = array("Test","Test_11","Test.11","TEST","test","()Test");

foreach ($test as $var){
	echo $var." - ";
	if(!preg_match("/^[A-Za-z0-9_]+$/", $var)) {
             echo "Doesn't validate";
	} else {  
             echo "Does validate"; 
        }
	echo "<br>";
}
?>

One using regex:

<?php
$test = array("Test","Test_11","Test.11","TEST","test","()Test");

foreach ($test as $var){
	echo $var." - ";
	if(ereg("[^A-Za-z0-9_-]+", $var)) {
             echo "Doesn't validate";
	} else {  
             echo "Does validate"; 
        }
	echo "<br>";
}
?>


#11 manmanman

manmanman
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 02 October 2006 - 09:06 PM

this should do it

if(ereg("[^A-Za-z0-9_-]+", $var)

been out of practice of regex for a while

Ok, reviewing this code, you have missing curly brackets.


notice his IF statements only have one statement...
When your IF statements only do one thing...you don't need curly braces


I'm a bit forgetful sometimes...

#12 Brentley_11

Brentley_11
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 02 October 2006 - 09:16 PM

Thank you for all the help. It works great. :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users