Checking an empty tag in POST -field by Javascript

How can you check an empty tag -field by Javascript like in asking questions at SO?

I would not like to pass the user to send a question without specifying the tag.

Upvotes: 0

Views: 241

Answers (1)

karim79
karim79

Reputation: 342635

To start you off, in your page:

function validateForm()
{
    var tags = document.getElementById('tags').value;
    if(tags == '' || tags == null) {
        alert('Please enter one or more tags');
        return false;
    }
    return true;
}

<form method="post" onsubmit="javascript:validateForm()">
<input type="text" id="tags" name="tags"/>
<input type="submit" value="Post your question"/>
</form>

In your PHP script:

if(isset($_POST['tags']) && !empty($_POST['tags'])) {
    $tags = $_POST['tags'];
}

Upvotes: 2

Related Questions