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

 

}

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.