Heriberto Juarez
Heriberto Juarez

Reputation: 271

How to know if there are spaces in an input using php

I have a form to get information and when i type just spaces with no any other character it gets posted, How could i avoid inserting just spaces in the form?

<form action="index.php" method="post">
    <textarea name="textA"></textarea>
    <input type="submit" name="sent" value="Send">
</form>
<?php
    if(isset($_POST['sent']) && !empty($_POST['textA'])){
        $insert=new Insert();
        $insert->insertData($_POST['textA']);
    }
?>

Upvotes: 2

Views: 102

Answers (1)

John Conde
John Conde

Reputation: 219834

Just use trim() which will remove all leading and trailing spaces. If there are only spaces in the string then it will become an empty string and empty() will be true.

if(isset($_POST['sent']) && !empty(trim($_POST['textA']))){

You can also do things like str_replace() to replace spaces with and empty string or preg_replace() to do the same but this should do what you need.

Upvotes: 6

Related Questions