Update: The steps in this post don’t entirely work as it is missing something as I realized later. Rather than update this post I’d like to point visitors to this post where I found out what I was missing. I am lazy that way. :)
I have a bit of code in one of my Flows that would check if a certain bit of text is all numbers or not. Basically it was part of a Flow wherein I’d take a mobile number input and I expect it to be in an E.164 format: +[code][digits]
. I don’t validate the number of digits (has to be less than 15 including the code I think) but I do strip it off spaces, dashes, brackets etc. and then I remove the + sign and want to validate if what remains is actually a number.
This is the bit in the Extract
section: if(greaterOrEquals(int(substring(variables('MobileNumber'), 1, sub(length(variables('MobileNumber')), 1))), 0), 'Int', 'Str')
Here’s what I do:
First I use substring
. This extracts a part of the string. Specifically, I want to remove the + sign (I know it is there as I validate that earlier in the Flow). So I want to extract everything after the first character up to the length of the string minus 1 (coz that what remains after the first character). That’s what this bit does: substring(variables('MobileNumber'), 1, sub(length(variables('MobileNumber')), 1))
Then I convert that to an integer using the int() function
. This is int(substring(variables('MobileNumber'), 1, sub(length(variables('MobileNumber')), 1)))
.
Then I check if this is greater than or equal to 0. This is my way of checking if the output of int() was actually an integer or not. I couldn’t think of any other way. Thus I do greaterOrEquals(int(substring(variables('MobileNumber'), 1, sub(length(variables('MobileNumber')), 1))), 0)
.
Finally I say if the previous check was try then output a string ‘Int’ else output ‘Str’. I can then check this in the following If
clause to determine if the text is a number of not.
Here’s how I remove all invalid characters earlier btw. First I define an array of the characters.
Then I loop over this and do a replace.
In the Compose
section I am doing replace(variables('MobileNumber'), items('FixMobile'), '')
. items('FixMobile')
refers to the current item in the loop, so essentially I am doing a replace of the MobileNumber
variable for each item and then assigning it back to MobileNumber
in Set variable
.
That’s it!