-1

I am basically new to html/css/javascript and I just got thrown a problematic issue.

I have a text box for my "Others, please specify" option. However, there are instances where some respondents can proceed to the next question by just entering a space and nothing else. When I download my data, the field became blank and hence more cleaning work is needed.

Just wondering if there any ways (html, css or javascript) that I can make use of to prevent my respondents from entering just spaces for the very first character. Any help would be appreciated. Thank you.

  • 2
    You can `.trim()` the entered data, and check whether it equals an empty string. Or you could look into using regex if your validation needs to be sophisticated – Nick Parsons Oct 17 '19 at 07:33
  • There are more then 1 way to do it, you can fetch the submit data before it's finally submitted and check it there, or you can add an OnInput Handler on the Textfield and check it there, or you just `trim()` it serverside. – Scriptkiddy1337 Oct 17 '19 at 07:34

2 Answers2

4

Put the required input in a form with a pattern which requires non-space characters:

<form>
  <input required pattern="\S.*">
  <button>submit</button>
</form>

\S.* means - match one non-space character (at the beginning of the string), followed by anything.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
0

Consider using RegEx and use your desired pattern. In your case /[^\s]/ (matches anything that is not a whitespace character) will do.

sibasishm
  • 443
  • 6
  • 24