Jump to content

[SOLVED] Check if user enters characters


allistera

Recommended Posts

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

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) {

 

}

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.

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))) {

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.