0

Try check if input filed start with blank space, after testing e few hours and searching and testing a lot of patterns, found on internet, it doesn't work.

What i want: check if html5 input field start with character (no blank space), but in the text are blank spaces allowed.

At the moment the best solution i found, but it doesn't work:

<input type="text" class="naamveld" placeholder="Fullname" pattern="^\S+$" required>

Someone can help me with this 'simple' thing?

Tuxy
  • 91
  • 10
  • What do you want to achieve? Sorry but your grammar is hard to read.... – Mr PizzaGuy Nov 04 '20 at 14:13
  • 2
    Couldn't you just trim() the input on form submission? – berkobienb Nov 04 '20 at 14:13
  • 1
    exactly i was going to say that – Mr PizzaGuy Nov 04 '20 at 14:13
  • 1
    Here's the answer to your question if you're adamant about doing it this way: https://stackoverflow.com/questions/54020591/not-allow-space-as-a-first-input-character-in-input-field – berkobienb Nov 04 '20 at 14:15
  • Does this answer your question? [Not allow space as a first input character in input field](https://stackoverflow.com/questions/54020591/not-allow-space-as-a-first-input-character-in-input-field) – berkobienb Nov 04 '20 at 14:15
  • Does this answer your question? [Javascript regex - no white space at beginning + allow space in the middle](https://stackoverflow.com/questions/19973669/javascript-regex-no-white-space-at-beginning-allow-space-in-the-middle) – Lelio Faieta Nov 04 '20 at 15:52

3 Answers3

1

You should use a regular expression like this:

^[^-\s][\w\s-]+$

like it is explained in this question

Lelio Faieta
  • 6,457
  • 7
  • 40
  • 74
0

According to your question, you said you wanted to remove all the whitespaces in the input, try this:

<input type="text" class="naamveld" placeholder="Fullname" required onkeydown="this.value = this.value.trim()" onkeyup="this.value = this.value.trim()">

If you try adding spaces at the beginning and end of the input, it will remove them.

How it works

The input has two attributes, (onkeydown and onkeyup) these functions will perform when the input receives a keyup or keydown, and will remove all whitespaces from the value of the input using this.value = this.value.trim()

If you want to learn more on how the trim() function works, you can go here.

Mr PizzaGuy
  • 410
  • 6
  • 19
  • OP is asking to check, not to remove. And to do this using the html5 pattern attribute. So he is looking for the correct regexp to be put in the pattern attribute, not to trim the field value – Lelio Faieta Nov 04 '20 at 14:28
  • oh... sorry I thought this was the question because the grammar was really bad and everyone in comments was suggesting to use `.trim()` – Mr PizzaGuy Nov 04 '20 at 14:29
0

First of all, again sorry for my bad english :-s I want thanks everyone for all this useful info. When looking to the answers and solutions, with our help this is the solution (see pattern):

<input type="text" pattern="^\S.*$" ...>

Checked if input field started with blank space(s).

This is for me the best and simplest solution :-)

Tuxy
  • 91
  • 10