MyTheory Posted April 7, 2008 Share Posted April 7, 2008 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! Quote Link to comment Share on other sites More sharing options...
p2grace Posted April 7, 2008 Share Posted April 7, 2008 Are you asking how to validate the data inside of each comma? You'd just explode the string into an array and validate each item. Quote Link to comment Share on other sites More sharing options...
MyTheory Posted April 7, 2008 Author Share Posted April 7, 2008 Nope, not the data inside.. I need make sure the user inputted a comma seperated list.. Quote Link to comment Share on other sites More sharing options...
inactive Posted April 7, 2008 Share Posted April 7, 2008 Is there any minimum requirements? Or say will "a,b" do? Quote Link to comment Share on other sites More sharing options...
MyTheory Posted April 7, 2008 Author Share Posted April 7, 2008 Erm it'd be nice if it validated the comma and the space afterwords, that's all I need please. Quote Link to comment Share on other sites More sharing options...
quiettech Posted April 7, 2008 Share Posted April 7, 2008 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; ?> Quote Link to comment Share on other sites More sharing options...
inactive Posted April 7, 2008 Share Posted April 7, 2008 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); ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.