phpretard Posted April 20, 2008 Share Posted April 20, 2008 I need some code that will validate a full name. Argument: 'John'=No Good | 'Doe'=No Good | 'John Doe'=Good if ($Name=='ONE WORD'){ $pass="No"; } if ($Name=='TWO WORDS'){ $pass="Yes"; } Anyone got an Idea Quote Link to comment https://forums.phpfreaks.com/topic/101980-solved-simple-validation/ Share on other sites More sharing options...
Fadion Posted April 20, 2008 Share Posted April 20, 2008 What i can think of is using explode() and count if there are 2 values on the array: <?php $input = 'John Smith'; $name = explode(' ', trim($input)); //trim() input if u have "John " or " John" if(count($name) == 2){ //if $name array has two values $pass = 'yes'; } else{ $pass = 'no'; } ?> or just see if there's any space: <?php $input = 'John Smith'; if(strstr(trim($input), ' ')){ //if there is a space between the text $pass = 'yes'; } else{ $pass = 'no'; } ?> I would prefer the first anyway. Quote Link to comment https://forums.phpfreaks.com/topic/101980-solved-simple-validation/#findComment-521892 Share on other sites More sharing options...
darkfreaks Posted April 20, 2008 Share Posted April 20, 2008 that will also work with str_word_count instead of count Quote Link to comment https://forums.phpfreaks.com/topic/101980-solved-simple-validation/#findComment-521893 Share on other sites More sharing options...
Fadion Posted April 20, 2008 Share Posted April 20, 2008 that will also work with str_word_count instead of count Didnt know str_word_count(), but guess its almost the same approach Quote Link to comment https://forums.phpfreaks.com/topic/101980-solved-simple-validation/#findComment-521896 Share on other sites More sharing options...
woobarb Posted April 20, 2008 Share Posted April 20, 2008 I like the str_word_count() function because it also handles comms ',' and the such, because some nationalities will write their surname before the given name, and it is proper to do this with a comma. Quote Link to comment https://forums.phpfreaks.com/topic/101980-solved-simple-validation/#findComment-521897 Share on other sites More sharing options...
phpretard Posted April 20, 2008 Author Share Posted April 20, 2008 Do you have an example of str_word_count. I am struggling to make it work. Quote Link to comment https://forums.phpfreaks.com/topic/101980-solved-simple-validation/#findComment-521904 Share on other sites More sharing options...
woobarb Posted April 20, 2008 Share Posted April 20, 2008 Are you aware of the php manual, that has some examples for each way of using it. The search box is the most invaluable resource available for php programming. Quote Link to comment https://forums.phpfreaks.com/topic/101980-solved-simple-validation/#findComment-521909 Share on other sites More sharing options...
phpretard Posted April 20, 2008 Author Share Posted April 20, 2008 Yes...I read it, and search google before I post (as descrided in site conditions). The code I keep running into puts it all into an array... I don' think I need an array to count words or do I? Quote Link to comment https://forums.phpfreaks.com/topic/101980-solved-simple-validation/#findComment-521912 Share on other sites More sharing options...
woobarb Posted April 20, 2008 Share Posted April 20, 2008 format Specify the return value of this function. The current supported values are: * 0 - returns the number of words found Quote Link to comment https://forums.phpfreaks.com/topic/101980-solved-simple-validation/#findComment-521916 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.