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! Link to comment https://forums.phpfreaks.com/topic/99905-solved-code-example-request-validate-words-are-comma-seperated/ 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. Link to comment https://forums.phpfreaks.com/topic/99905-solved-code-example-request-validate-words-are-comma-seperated/#findComment-510887 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.. Link to comment https://forums.phpfreaks.com/topic/99905-solved-code-example-request-validate-words-are-comma-seperated/#findComment-510888 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? Link to comment https://forums.phpfreaks.com/topic/99905-solved-code-example-request-validate-words-are-comma-seperated/#findComment-510893 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. Link to comment https://forums.phpfreaks.com/topic/99905-solved-code-example-request-validate-words-are-comma-seperated/#findComment-510898 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; ?> Link to comment https://forums.phpfreaks.com/topic/99905-solved-code-example-request-validate-words-are-comma-seperated/#findComment-510914 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); ?> Link to comment https://forums.phpfreaks.com/topic/99905-solved-code-example-request-validate-words-are-comma-seperated/#findComment-510916 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.