Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Salesforce by (11.9k points)

I need a regex to limit number digits to 10 even if there are spaces.

For example it allows 06 15 20 47 23 the same as 0615204723.

I tried: ^\d{10}$, but how do I ignore spaces?

Also, the number should start with 06 or 07. (Editor's note, the last requirement was from comments by the OP.)

1 Answer

0 votes
by (32.1k points)

Since you need a regex to limit number digits at 10, in javascript, the following code will work:

var s = ' 06 15 20 47   34 ';

s = s.replace (/\s/g, "");

isValid = /^0[67]\d{8}$/.test (s);

To get you a more generalized answer, you can use the following code and alter it according to the language you're using:

/^\s*(0\s*[67]\s*(?:\d\s*){8})$/

Related questions

0 votes
1 answer
0 votes
1 answer
asked Nov 12, 2020 in Python by ashely (50.2k points)
0 votes
1 answer
+1 vote
1 answer

Browse Categories

...