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. Quote Link to comment 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 } Quote Link to comment 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) { } Quote Link to comment 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. =) Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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))) { Quote Link to comment 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 ?> Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
conker87 Posted April 22, 2008 Share Posted April 22, 2008 Thank you! Works now No problem 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.