简体   繁体   中英

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"/>

[^\\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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM