0

I was wondering if there is a way to know if an text input has been filled using JS

For example I have a basic text input :

<input type="text" name="textInput" id="form">

Here is the JS code I use to fill :

document.getElementById("form").value = "Some information";

And I'd like to know by any way if the input has been filled using JS to stop the sign-up for example. I have NO idea how to proceed.

user14063792468
  • 839
  • 12
  • 28
Lenr
  • 25
  • 1
  • 9
  • In your example you show how to set a value, knowing how to do that, how would you get the value? Once you get the value how do you check a string to see if it’s empty? What have you tried? – Alexander Staroselsky Jul 07 '19 at 21:06

2 Answers2

1

I think you could check if defaultValue property is different from the value:

const input = document.getElementById("form")

input.value = "Some information";

const setWithJS = input.value !== input.defaultValue

alert(setWithJS)
<input type="text" name="textInput" id="form">

The idea here is that defaultValue will be set when HTML is initially parsed (value attribute). defaultValue will not reflect changes to value made programmatically.

dfsq
  • 191,768
  • 25
  • 236
  • 258
-1

Yes you can check this by using if condition that the field is filled or not

If(condition check){ Block of code }

Manu
  • 23
  • 5