1

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"/>
3
  • Perhaps the pattern attribute doesn't need delimiters? Commented May 9, 2013 at 0:43
  • I edited the code above, I did not have delimiters when I tested. but still no luck Commented May 9, 2013 at 0:48
  • 1
    It would be easier if you just add the required attribute. That will prevent empty value submissions Commented May 9, 2013 at 1:17

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.