Determine if a string is a "number"

Im trying to determine if an input if is a number.

If I use:

If val(field.text) >0
//yes
end if

However if the user puts “5 Star Plumbing” or “1st National Bank” Xojo see it as a value of 5 and 1 respectively.

Any easy way to do this?

Doh!.

Just walk the string looking for charters of 0-9 and perhaps the Decimal

Use a regular expression.

[quote=107946:@Jay Menna]Im trying to determine if an input if is a number.

If I use:

If val(field.text) >0
//yes
end if

However if the user puts “5 Star Plumbing” or “1st National Bank” Xojo see it as a value of 5 and 1 respectively.

Any easy way to do this?[/quote]

Zero is a number too.

Why not use a mask to forbid anything but digits and dot ?

Or, before using Val, check if the input contains anything that is not a digit or coma, and if this is the case you know it cannot be a number.

Something like

if format(val(Me.text),"###########.##") <> Me.Text then //not number End if

Don’t forget negative/positive.

But use a regular expression.

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

Off the top of my head.

uh…

if isNumeric(s) then

Dave is right. I was originally thinking of checking for integer only, then ended up overdoing my pattern.

For integer only:

^[+-]?\\d+$

Be careful with “Not a number”

IsNumeric(“nan”)

returns true and obviously “nan” is not a number for mortals, although it is for Xojo.

1 Like