11

I'm using happyJS and use the regex underneath for phone validation

phone: function (val) {
        return /^(?:[0-9]+$)/.test(val);
    }

However this ONLY allows numbers. I want the user to be able to enter spaces as well like

238 238 45383

Any idea why return /^(?:[0-9 ]+$)/.test(val); is not doing the trick?

matt
  • 42,713
  • 103
  • 264
  • 397
  • 5
    `/^(?:[0-9 ]+$)/.test("238 238 45383");` returns true. Seems to work. – Anirudh Ramanathan Nov 05 '12 at 10:18
  • @raina77ow Well, you were absolutely right! I'm sorry. I just found out that there is some server-side caching going on that wouldn't reload my script file. I tested it a 100 times but it wouldn't show an effect. Now everything seems to work fine. Any yeah, your right - my provided code works perfectly fine. – matt Nov 05 '12 at 10:42

5 Answers5

10

This is my suggested solution:

/^(?=.*\d)[\d ]+$/.test(val)

The (?=.*\d) asserts that there is at least one digit in the input. Otherwise, an input with only blank spaces can match.

Note that this doesn't put any constraint on the number of digits (only makes sure there are at least 1 digit), or where the space should appear in the input.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
  • @raina77ow: For `?:` not really important, I guess. I just mimic OP's code. And your tests are simply wrong and worse than OP's solution. `/[0-9]/` will check there exist a digit, `/^[0-9 ]/` checks there exists a digit or space at the beginning of the string. Even if you add `+` behind, they are still wrong. – nhahtdh Nov 05 '12 at 10:24
  • @nhahtdh It should have been `/[^0-9 ]/`, of course. The point is it's easier to build (and maintain) two simple things working together than one complex thing in development - and regexes are (usually) no exception for that. – raina77ow Nov 05 '12 at 10:40
  • @raina77ow: I understand your intention in writing that regex (logical NOT the test result), but empty string or string with only space will pass the test. – nhahtdh Nov 05 '12 at 10:45
  • Empty string would pass `/[0-9]/ && ! /[^0-9 ]/` test? – raina77ow Nov 05 '12 at 10:46
  • @raina77ow: I think I misunderstood your original comment (which was deleted). I understand what you say now, and I think you can write your own answer. – nhahtdh Nov 05 '12 at 10:52
6

Try

phone: function (val) {
    return /^(\s*[0-9]+\s*)+$/.test(val);
}

At least one number must be present for the above to succeed but please have a look at the regex example here

Bruno
  • 5,772
  • 1
  • 26
  • 43
1

Try

/^[\d ]*$/.test("238 238 45383")

console.log(/^[\d ]*$/.test("238 238 45383"));
Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345
1

You can try the below regex for checking numbers and spaces.

function isTextAndNumberSpaceOnly(text) {
    var regex = /^[0-9 ]+$/;
    if (regex.test(text)) {
        return true;
    } else {
        return false;
    }
}
amit pandya
  • 1,384
  • 13
  • 22
0

Personally I use this code and it works properly:

function validateMobile(mob) 
{ 
     var re = /^09[0-9]{9}$/
     if(mob.match(re))
         return true;
     else
        return false; 
}
lukehillonline
  • 2,430
  • 2
  • 32
  • 48
  • The phone number format varies between countries. The prefix 09 in your regex has already made some assumption about the number format. – nhahtdh Nov 05 '12 at 10:27