Jump to content

Archived

This topic is now archived and is closed to further replies.

Brentley_11

Simple Question... I think [Solved]

Recommended Posts

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.

Share this post


Link to post
Share on other sites
[code]
if(!ereg("[A-Za-z0-9_-]*",$_POST['yourVar]))
  echo "Your POST var doesn't validate";
[/code]

Share this post


Link to post
Share on other sites
Is there a chance that I have php configured wrong? I just tested what you said and it didn't work.

[code]$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>";
}[/code]

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

The third and last one shouldn't validate.

Share this post


Link to post
Share on other sites
Everything still validates.  ???

Code:
[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>";
}[/code]

Output:
[code]Test2
Test - Does validate
Test_11 - Does validate
Test.11 - Does validate
TEST - Does validate
test - Does validate
()Test - Does validate[/code]

Share this post


Link to post
Share on other sites
[quote author=zanus link=topic=110288.msg445576#msg445576 date=1159820889]
[code]
if(!ereg("[A-Za-z0-9_-]*",$_POST['yourVar]))
  echo "Your POST var doesn't validate";
[/code]

[/quote]

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.

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

Share this post


Link to post
Share on other sites
And also a [color=red]-[/color] is currently allowed,

[code]

<?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>";
}

?>
[/code]

Share this post


Link to post
Share on other sites
[quote author=Brentley_11 link=topic=110288.msg445580#msg445580 date=1159821415]
Is there a chance that I have php configured wrong? I just tested what you said and it didn't work.

[code]$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>";
}[/code]

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

The third and last one shouldn't validate.
[/quote]

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

[code]<?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>";
}
?>[/code]

Share this post


Link to post
Share on other sites
this should do it
[code]if(ereg("[^A-Za-z0-9_-]+", $var)[/code]

been out of practice of regex for a while

[quote]
Ok, reviewing this code, you have missing curly brackets.
[/quote]

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

Share this post


Link to post
Share on other sites
Try this; it works on my server:

[code]
<?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>";
}
?>
[/code]

One using regex:

[code]
<?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>";
}
?>
[/code]

Share this post


Link to post
Share on other sites
[quote author=zanus link=topic=110288.msg445591#msg445591 date=1159822713]
this should do it
[code]if(ereg("[^A-Za-z0-9_-]+", $var)[/code]

been out of practice of regex for a while

[quote]
Ok, reviewing this code, you have missing curly brackets.
[/quote]

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

I'm a bit forgetful sometimes...

Share this post


Link to post
Share on other sites

×

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.