Jump to content

[SOLVED] Code Example Request : Validate Words Are Comma Seperated


MyTheory

Recommended Posts

Hi if someone could post an example php code for the following I'd be grateful.

 

So the scenario is that, I invite a user to enter a string of words into a form input box, such as "MyTheory, pocobueno1388, darkfreaks, bad_gui, JustGotAQuestion, cainmi, KEFE, didgydont, steviez, Helminthophobe, studgate, rofl90, EchoFool, ifm1989", I'd like to see an example of validating the  comma seperation.. if someone could post one they have i'd appreciate it.

 

Thanks guys!

Will only validate a comma-space delimited list containing the following characters:

 

a-z and A-Z

0-9

_ (underscore)

- (dash)

 

<?php
$data = 'MyTheory, pocobueno1388, darkfreaks, bad_gui, JustGotAQuestion, cainmi, KEFE, didgydont, steviez, Helminthophobe, studgate, rofl90, EchoFool, ifm1989';
$result = eregi('^([a-z0-9_\-]+, )+[a-z0-9_\-]+$', $data) ? 'valid' : 'invalid';
echo $result;
?>

Or else this is slightly more cumbersome, but will validate any characters, provided each part is separated by a comma and a space:

<?php
function is_it_good($entered_data) { //////////

$test = explode(',',$entered_data);

if (count($test) > 1) {
    $goodcount = 1;
    foreach ($test as $i => $data) {
            if ($data[0] == ' ') {
                $goodcount++;
            }
    }
    if ($goodcount >= count($test)) {
        $result = 1;
    } else {
        $result = 0;
    }
} else {
    $result = 0;
}
if ($result == 1) {
    echo 'Thats OK!';
} else {
    echo 'Thats not working fool!';
}
} /////////////////////////////////////////////

$entered_data_1 = "abcdef";
$entered_data_2 = "abobo booyah chilli";
$entered_data_3 = "MyTheory, pocobueno1388, darkfreaks, bad_gui, JustGotAQuestion, cainmi, KEFE, didgydont, steviez, Helminthophobe, studgate, rofl90, EchoFool, ifm1989";

echo $entered_data_1.'<br />';
is_it_good($entered_data_1);
echo '<br /> <br />';

echo $entered_data_2.'<br />';
is_it_good($entered_data_2);
echo '<br /> <br />';

echo $entered_data_3.'<br />';
is_it_good($entered_data_3);

?>

Archived

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

×
×
  • Create New...

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.