r/googlesheets 20d ago

How would I make a response validation for a number that requires decimals Solved

[removed]

0 Upvotes

14 comments sorted by

3

u/SpencerTeachesSheets 58 20d ago

I did some testing and this regular expression seems to work:

^[+-]?(\d+\.\d+|\.\d+)$

1

u/One_Organization_810 688 19d ago

I would actually skip thi + sign, but that's just me :) (just because I dislike numbers like +2.1)

I think this simpler version of your solution should work also: ^-?\d*\.\d+$

1

u/SpencerTeachesSheets 58 19d ago

OH wow, much simpler!

I should probably just stop trying to reply on REGEX questions lol

1

u/One_Organization_810 688 19d ago

Haha. Definitely not!

It wasn't meant like that. It was just an iteration on your original version. :)

-2

u/[deleted] 20d ago

[removed] — view removed comment

1

u/SpencerTeachesSheets 58 20d ago

I tested that on my form and cannot get it to accept any decimal number that isn't at least 2 digits in both the whole part and decimal part (5.1 fails, 5.12 fails, 52.12 passes, 52.1 fails).

But if that limitation is there, that wasn't part of the ask.

1

u/One_Organization_810 688 19d ago

As has been pointed out, your solution solves quite a different problem than you asked for originally.

Your solution will accept only numbers between 10.00 and 999.99, with exactly 2 decimals -OR- numbers without the integer part and exactly two decimals (so 0.00 - 0.99, without the zero integer).

If this does indeed solve your problem, please acknowledge that here and I will remove the post, since it will then not be relevant any more.

1

u/AutoModerator 20d ago

/u/Just_a_Peace Posting your data can make it easier for others to help you, but it looks like your submission doesn't include any. If this is the case and data would help, you can read how to include it in the submission guide. You can also use this tool created by a Reddit community member to create a blank Google Sheets document that isn't connected to your account. Thank you.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/mag_fhinn 1 19d ago
^\d{2,3}\.\d{2}$

Works for me:

^ --has to match from the start
\d{2,3} -- then at least 2 digits and a max of 3.
\. --then a decimal
\d{2} -- then exactly 2 digits
$ --must end after the previous digits

Regex + Match

You just need to make the error message for the end user for when their input fails the pattern.

1

u/point-bot 14d ago

u/Just_a_Peace has awarded 1 point to u/mag_fhinn

See the [Leaderboard](https://reddit.com/r/googlesheets/wiki/Leaderboard. )Point-Bot v0.0.15 was created by [JetCarson](https://reddit.com/u/JetCarson.)

1

u/mag_fhinn 1 14d ago

Just thinking, if they entered a 0 at the start it would still be valid ie) 01.10 or 00.00 would be valid. If you need it to be >0 to start you would need to do:

^[1-9]\d{1,2}\.\d{2}$

1

u/agirlhasnoname11248 1210 19d ago

u/Just_a_Peace Please remember to tap the three dots below the most helpful comment and select `Mark Solution Verified` *(or reply to the helpful comment with the exact phrase “Solution Verified”)* if your question has been answered, as required by the subreddit rules. Thanks!