brain storm
brain storm

Reputation: 31252

regex pattern matching in HTML5 is not working properly

I have an input text box in my HTML form which looks for a regex pattern as shown below. I am looking for anything to be entered other than white spaces or blank. I tried all the following below and none of them is allowing me to enter any normal text such as "hello world" and "helloworld" in it.Any suggestions are most welcome. Thanks

<input name="item" type="text" size="25" autofocus="autofocus" pattern="^\S$" title="Enter something valid"/>

<input name="item" type="text" size="25" autofocus="autofocus" pattern="^[^\s]*$" title="Enter something valid"/>

<input name="item" type="text" size="25" autofocus="autofocus" pattern="^[\S]*$" title="Enter something valid"/>

EDIT:

after removing the anchor, this works for "helloworld" but not for "hello world". So I think it has to do with regex pattern.

<input name="item" type="text" size="25" autofocus="autofocus" pattern="[^\s]*" title="Enter something valid"/>

Upvotes: 1

Views: 2788

Answers (1)

sync
sync

Reputation: 5620

[^\s]* will match against anything that contains no spaces, so a space in the words will not match.

You probably want something like .*[^\s].* to match a string with at least one non-space character.

The required attribute is probably the best way to guard against blanks (or ^$ should work).

Upvotes: 1

Related Questions