Check if a string contains only Numbers or not

Hello everyone,

I have these 3 strings “120”, “120a” and “Test”.

Lets consider the first case “120”, here all 3 are numbers so i am good and can go with then condition.
and second case “12o”, here 1 and 2 are numbers. and o is string, then i should go with else condition.
and third case “Test”, here the entire string is text, so have to go with else condition.

Then condition only if the entire string contains Numbers.

what condition should i put within my IF?

Thank you

Hi @hansen_Lobo

Try this in IF condition

System.Text.RegularExpressions.Regex.IsMatch(InputString, "\b\d+\b")

image

1 Like

@hansen_Lobo

system.Text.RegularExpressions.Regex.IsMatch(inputstr,“\b\d+\b”)

gives you true value when there is number otherwise false

1 Like

@hansen_Lobo

Hope the below image helps you

1 Like

grafik
vs
grafik

but recommended to trim the test string also

^\d+$

This works for all my test scenarios. Thank you. Will make sure to trim the variable before I pass it to

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.