vlevsha
vlevsha

Reputation: 265

Textarea validation

My script is supposed not to let the HTML form be submitted unless a user enters some data into a textarea control. However, the form gets submitted anyway and no message for the user is displayed. Could anyone see what I'm doing wrong?

Here is my HTML:

<textarea name="description" id="description" rows = "4" cols = "25">
    <?php echo $description?>
    </textarea>

As for PHP, neither this:

if ($_POST['description'] == "") {
            $errors .= 'Please enter a description of your invention.<br/><br/>';
        }

or this:

if (empty($_POST['description'])) {
            $errors .= 'Please enter a description of your invention.<br/><br/>';
        }   

work.

Upvotes: 1

Views: 11252

Answers (3)

You have some white space between the <textarea> tags. Have you tried outputting $_POST['description'] to see if this is being posted back? The extra new lines may be messing it up. You can try adding a minimum length to it.

Upvotes: 0

mellowsoon
mellowsoon

Reputation: 23241

That's because the text area isn't empty! Note that you're putting a tab before <?php echo $description?>, which means at the very least the text area will always contain a tab.

Change your validation code to something like this:

if (trim($_POST['description']) == "") {

Upvotes: 8

robbrit
robbrit

Reputation: 17960

In your form put:

<form onsubmit = "return checkSubmit()">

Then in your Javascript somewhere:

function checkSubmit(){
  if (form good)
    return true;
  else
    return false;
}

Upvotes: 0

Related Questions