Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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, ...}

share|improve this question
    
Please, elaborate on your last statement. 1+?? –  Ravi Thapliyal May 20 '13 at 10:29
    
must your pattern match 1-2? –  Casimir et Hippolyte May 20 '13 at 10:42

3 Answers 3

up vote 2 down vote accepted

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.

share|improve this answer
    
thnx this is what i was looking for –  Naveen May 20 '13 at 10:39
    
Please consider accepting the answer unless you have any follow up question. –  Ravi Thapliyal May 20 '13 at 10:41
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
   }
share|improve this answer

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..

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.