Search code examples
phpstringconditional-statementsformfield

In PHP how can I detect when a user is deleting text from an input text field


<form action="" method="POST">
<input type="text" name="phonenumber" value="" />
<form>

If there is something written in the input text field and the user start to delete the string meaning the last letter of the string I want to do something.

So something like this:

if(user deletes last letter of isset($_POST['phonenumber'])){
//do something
}

I tried to use strlen() with -1 but did not seem to work. Anyone have any ideas?

Thank you!


Solution

  • You should use javascript to do something when user is deleting char. One way is to listen to 'keyup' event of the text input. E.g.:

    <script>
    $(":text[name=phonenumber]").on('keyup', function(event){
       if (event.keyCode == 8){ // means BACKSPACE pressed
            // do something
       }
    });
    </script>