This uses the old JavaScript approach. You can use jquery, but if thatβs all you want to do, it will be redundant. In the code below, the oninput event is oninput when the text is changed either from the keyboard or with the mouse / tap (you need to write everything, this should do it for you). Then javascript receives the contents of the text field, removes the previous and trailing spaces and sets the disabled attribute of the submit button to an empty text box with the qualification .length == 0 .
Note. If you do not want to disable text that only has a space change submit.disabled = (tb.value.trim().length == 0); before submit.disabled = (tb.value.length == 0); , which eliminates the call to the trim () method. The trim () method breaks previous and trailing spaces.
This approach eliminates the need for postback to enable or disable the submit button.
<!doctype html> <html> <head> <title>Demo</title> <script type="text/javascript"> var enableInput = function() { var tb = document.getElementById("text_box"); var submit = document.getElementById("submit"); submit.disabled = (tb.value.trim().length == 0); }; </script> </head> <body> <form> <input type="text" id="text_box" name="test" oninput="enableInput();"></input><br/> <input id="submit" type="submit"></input> </form> </body> </html>
source share