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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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);

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.