Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Data Validation in Google Sheets to only allow numbers, commas and spaces

I am creating an input google sheet to accept just numbers, commas & spaces - examples listed below. - At a basic level, I just want to exclude the use of A-Z / a-z.

80092382
800
800,876
98672102,20192210

I would like it to exclude anything like:

Hey 01
987 blue
black 1 white
orange

I am getting stuck early on, where I'm trying to only allow text with only numbers in, or excluding anything with letters in. I have tried the following lines of code within the RegexMatch formula but it either accepts lines with text, or rejects my numbers.

=RegexMatch(L5,"\d")
- This one rejects the numbers.
    
=RegexMatch(to_text(L5),"\d")
- This rejects only where there are no numbers in the cell - So 'Hey 01' is accepted.

=RegexMatch(to_text(L5),"^\d")
- Same issue, if the cell starts with a number '987 blue' then its accepted

I've attempted a few other ways, such as using the NOT function at the beginning & using other regular expressions. If anyone can point me in the right direction then that would be much appreciated.

Test sheet

like image 988
Paul Marvell Avatar asked Oct 22 '25 16:10

Paul Marvell


1 Answers

You can use

=ARRAYFORMULA(IF(REGEXMATCH(TO_TEXT(C2:C9), "^\d+(,\s*\d+)*$"), "Good", "Error"))

The regex matches

  • ^ - start of string
  • \d+ - one or more digits
  • (,\s*\d+)* - zero or more repetitions of
    • , - a comma
    • \s* - zero or whitespace
    • \d+ - one or more digits
  • $ - end of string.
like image 67
Wiktor Stribiżew Avatar answered Oct 25 '25 05:10

Wiktor Stribiżew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!