JS: regex for numbers and spaces?
Asked Answered
G

5

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?

Genniegennifer answered 5/11, 2012 at 10:14 Comment(2)
/^(?:[0-9 ]+$)/.test("238 238 45383"); returns true. Seems to work.Fortalice
@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.Genniegennifer
J
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.

Janis answered 5/11, 2012 at 10:22 Comment(5)
@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.Janis
@Janis 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.Tammietammuz
@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.Janis
Empty string would pass /[0-9]/ && ! /[^0-9 ]/ test?Tammietammuz
@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.Janis
W
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

Windbound answered 5/11, 2012 at 10:20 Comment(3)
lol this FORCES a space to be present, as opposed to allowing optional spaces. use this instead: return /^([0-9\s)+/.test(val);Bueno
@AndyLorenz Please check your regex as it doesn't actually work :)Windbound
well spotted: return /^([0-9\s]+)$/.test(val);Bueno
M
1

Try

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

console.log(/^[\d ]*$/.test("238 238 45383"));
Multiplicity answered 25/9, 2019 at 11:14 Comment(0)
P
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;
    }
}
Purtenance answered 21/4, 2020 at 6:56 Comment(0)
O
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; 
}
Oneida answered 5/11, 2012 at 10:23 Comment(1)
The phone number format varies between countries. The prefix 09 in your regex has already made some assumption about the number format.Janis

© 2022 - 2024 — McMap. All rights reserved.