Jump to content

Form validation


anevins

Recommended Posts

Hi there,

 

I've got a HTML textarea with the name attribute of 'review' and I've posted this into a variable called $review.

I want to say if the characters are less than 30, please enter more words etc.

 

The problem is, I get the if statement's error even when there are more than 30 characters.

 

Can anyone figure out why I'm getting this?

if ($review < 30){
echo '<p class="red">Review is too short, please enter at least 15 words</p>';
}

Link to comment
Share on other sites

I've now got this:

if (strlen($review) > 30){
echo '<p class="red">Review is too short, please enter at least 15 words</p>';
}

 

but I can't get the error message to appear now; if the characters are less than 30

Link to comment
Share on other sites

Echo $review to see if the variable is getting the string. Also print_r the post array to make sure the text areas value is getting registered. Thats a start...

 

I jusy forgot. Pika's right. U wanna use the strlen function, unless of course $review is an integer.

Link to comment
Share on other sites

Hi again,

I'm trying to do some form validation.

 

I want to say, if the input in the textarea is greater than 30 and less than 255 characters, do some stuff.

But I think something's wrong with my current if statement:

if ((strlen($review) < 255) && strlen(($review) > 30)){
...
}

 

As when I change the if statement to if(!empty($review)){ ... }, the if statement works.

 

Can you see what's wrong with my strlen if statement?

Link to comment
Share on other sites

You have a second parenthesis after the second strlen ("strlen((") I think you meant to put it after the second strlen ("strlen($review))"). In any case, its rather needless to enclose both strlen's in parenthesis. I would just write it as:

<?php
if (strlen($review) < 255 && strlen($review) > 30) {
//blahblahblahblah
} else {
//blahblahblahblah
}
?>

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.