2

I only want alphanumeric, numbers, space, dash and underscore. I am using

[\w\s-_]*

But because I have allowed spaces, a space is allowed as input, in html5 how can i validate if only spaces are entered as input.

Also is there any regular expression checking integers for 1+.

Edit:

allowing integers from 1 to any positive integers {1, 2, 3, ...}

2
  • Please, elaborate on your last statement. 1+?? Commented May 20, 2013 at 10:29
  • must your pattern match 1-2? Commented May 20, 2013 at 10:42

3 Answers 3

2

Use the following pattern that accepts a space only when surrounded by some other valid input characters as well. Please, note that \w already includes an _ underscore.

[\w-]+(\s[\w-]+)*

To match all positive integers >= 1 use the regex

^(?!0)\d+

The above excludes 0.

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

1 Comment

thnx this is what i was looking for
0

Here you need to write a little javascript to find out if there are only spaces. Get the input text then trim it and find if it's empty..

Comments

0
this [link][1] will help you 
 OR try this 
   var str = document.getElementById("myInput").value;
   if (str.match(/^\s*$/)) {
    // nothing, or nothing but whitespace
   } else {
    // something
   }

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.