Skip to content
Advertisement

Check if a string contains only number

I am trying to check if a string contains only valid number in the following format

But it should reject anything that contains non-numbers including double dots. Here are some invalid formats

I have done the following

When seems to work except for the case where there is more than one dot in the string.

How can I tweak the regular expression to only allow one dot otherwise return ‘Invalid’?

Advertisement

Answer

I would suggest try_convert():

The one possible downside is exponential format; 1e6 is a valid number for instance.

An alternative is the where approach; you just need more complete logic:

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement