allistera Posted April 22, 2008 Share Posted April 22, 2008 Hey there, in regards to my blog: http://allistera.110mb.com/blog/index.php In the comments section I have used these if statement to see if the user has entered something: if (strlen($comment) < 2) { But the user can still enter a space " ", and that is counted as a character, so the user can still enter something. Can you tell me of a way that it checks if a character/number is entered. Link to comment https://forums.phpfreaks.com/topic/102354-solved-check-if-user-enters-characters/ Share on other sites More sharing options...
GingerRobot Posted April 22, 2008 Share Posted April 22, 2008 How about: if(empty(trim($comment)){ //comment empty }else{ //process comment } Link to comment https://forums.phpfreaks.com/topic/102354-solved-check-if-user-enters-characters/#findComment-524094 Share on other sites More sharing options...
DarkWater Posted April 22, 2008 Share Posted April 22, 2008 Hey there, in regards to my blog: http://allistera.110mb.com/blog/index.php In the comments section I have used these if statement to see if the user has entered something: if (strlen($comment) < 2) { But the user can still enter a space " ", and that is counted as a character, so the user can still enter something. Can you tell me of a way that it checks if a character/number is entered. if (eregi(^[[:alnum:]]{2,}$, $comment) { } Link to comment https://forums.phpfreaks.com/topic/102354-solved-check-if-user-enters-characters/#findComment-524096 Share on other sites More sharing options...
DarkWater Posted April 22, 2008 Share Posted April 22, 2008 Woops: if (eregi('^[[:alnum:]]{2,}$', $comment) { } Missed quotes. =) Link to comment https://forums.phpfreaks.com/topic/102354-solved-check-if-user-enters-characters/#findComment-524100 Share on other sites More sharing options...
GingerRobot Posted April 22, 2008 Share Posted April 22, 2008 I certainly wouldn't use that regular expression. 1.) It would block punctuation 2.) Regex is (relatively) slow Link to comment https://forums.phpfreaks.com/topic/102354-solved-check-if-user-enters-characters/#findComment-524102 Share on other sites More sharing options...
DarkWater Posted April 22, 2008 Share Posted April 22, 2008 I certainly wouldn't use that regular expression. 1.) It would block punctuation 2.) Regex is (relatively) slow Good point, forgot it was a comment field. I thought it was like, a username field until I saw "$comment". @_@ Too lazy to fix it. Link to comment https://forums.phpfreaks.com/topic/102354-solved-check-if-user-enters-characters/#findComment-524103 Share on other sites More sharing options...
allistera Posted April 22, 2008 Author Share Posted April 22, 2008 Ok I used GingerRobot method but I keep getting back: Fatal error: Can't use function return value in write context in C:\wamp\www\blog\postcomment.php on line 24. Here is my whole if else statement: if (empty(trim($comment))) { echo "Ok there was an error!"; } else { // Add the comment to the database mysql_query("INSERT INTO `comments` (`name`,`date`,`ip`,`comment`,`artId`,`time`) VALUES ('$name','$currentDate','$IPAddress','$comment','$articleID','$currentTime');", $connectionString) or die ("Error code: 006, Please try again later"); // Go to the prev page, comments section header("Location: $prevPage#comments"); } Line 24 being: if (empty(trim($comment))) { Link to comment https://forums.phpfreaks.com/topic/102354-solved-check-if-user-enters-characters/#findComment-524123 Share on other sites More sharing options...
conker87 Posted April 22, 2008 Share Posted April 22, 2008 Try: <?php $comment = trim($comment); if (empty($comment)) { //etc ?> Link to comment https://forums.phpfreaks.com/topic/102354-solved-check-if-user-enters-characters/#findComment-524184 Share on other sites More sharing options...
allistera Posted April 22, 2008 Author Share Posted April 22, 2008 Try: <?php $comment = trim($comment); if (empty($comment)) { //etc ?> Thank you! Works now Link to comment https://forums.phpfreaks.com/topic/102354-solved-check-if-user-enters-characters/#findComment-524194 Share on other sites More sharing options...
conker87 Posted April 22, 2008 Share Posted April 22, 2008 Thank you! Works now No problem Link to comment https://forums.phpfreaks.com/topic/102354-solved-check-if-user-enters-characters/#findComment-524199 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.