Text field - restrict input to numeric data type

I could probably work this out, but in a long convoluted way.

What’s the simplest method to ensure that the user can only input a decimal number?

[EDIT] I’ve looked at the the event handler ‘ValidationError’ but that would require some work using the ‘mask’ property.

Add a keydown event handler and check that the key is a decimal digit.
If you also want to allow a minus sign and/or decimal point, then it gets more complicated, because there can be only one minus sign (which must be the first character), and only one decimal point.

Or possibly, you could call IsNumeric on the partially entered text after each keystroke.

Thanks Robert, I was hoping it would be easier - somehow.

I’ve used a few other programming languages some time ago. I’m trying to remember how this was handled.

I do remember in FileMaker you could choose a datatype, ie. number or text etc, and if the wrong type was input, then a message came up - I think.

Surely this is Xojo101.

I was thinking this would be done using the ‘lost focus’ event handler?

What is wrong with mask ?

I’m not sure.

I’ve used mask for formatting numeric labels but not with a text field input box.

Is there a simple way?

Mask is supposed to be the simple way.

Otherwise use Keydown to filter all entries that are not digits or dot.

Like I said, I’m sure I can work it out a long way. Ok, then I’ll check out mask.

I was hoping not to spend the next few hours mucking around with something that should be so basic.

I don’t think it will take hours.

Well, almost 1hr.

I think this works ok.

I’ve used IsNumeric in the lost focus event of the text field - I’m happy with the way this works.

[code] dim farkleberryjuice As Boolean

farkleberryjuice = IsNumeric (Me.Text)

If farkleberryjuice then
MsgBox (“Congratulations, You’ve entered some numeric data”)

ElseIf NOT farkleberryjuice Then
MsgBox (“What?.. I’m not happy!!.. Please enter some numeric data”)

End If[/code]

Upon testing, I haven’t had a failure yet but there may be some other complications and issues that I’m not aware of.

As it stands, your code is kind of punitive, whereas you could simply filter out any non numeric input during entry in Keydown, so there would be no way to enter non numeric :

Function KeyDown(Key As String) Handles KeyDown as Boolean If Not IsNumeric(key) And key <> "." Then Return True End Function

With my program, every keystroke does not have to be monitored. A simple evaluation upon exit is requirement enough.

[quote=311382:@Michel Bujardet]As it stands, your code is kind of punitive, whereas you could simply filter out any non numeric input during entry in Keydown, so there would be no way to enter non numeric :

Function KeyDown(Key As String) Handles KeyDown as Boolean If Not IsNumeric(key) And key <> "." Then Return True End Function [/quote]

That code should quite happily accept “95.45…64…34.5” :stuck_out_tongue_winking_eye:

Better:

[code]dim farkleberryjuice As Boolean = IsNumeric (Me.Text)

If farkleberryjuice then
MsgBox (“Congratulations, You’ve entered some numeric data”)
Else
MsgBox (“What?.. I’m not happy!!.. Please enter some numeric data”)
End If[/code]

Somewhat better: put this code in the keyDown event of the TextField (adapted from http://scispec.ca/index.php/computers):

[code]// This example allows the user to type in the numbers 0 through 9, the period, minus, backspace
// leftarrow, rightarrow and delete key.
//
// returning true means ignore the new key
//
// backspace = 8, delkey = 127, leftarrow=28, rightarrow=29, minus = 45, period = 46

//Let the user press these buttons and display them in the Editfield
if instr(“0123456789.-”+chr(8)+chr(28)+chr(29)+chr(30)+chr(127),key)=0 then return true

// Make sure that only one ‘period’ exists in the editfield
if CountFields(me.Text,".") > 1 and key=chr(46) then return true

// Make sure that only one ‘-’ exists in the editfield and that it can only be at the left end
if CountFields(me.Text,"-") > 1 and key="-" then return true
if key = “-” and me.SelStart > 0 then return true

//This can display the Ascii code that you pressed on your keyboard
//MsgBox cstr(asc(key))[/code]

I know my method of coding is a bit verbose but what difference does that make to the compiler?

It makes a difference to understanding the code later. Keep it simple. Double negations are sources for trouble.

http://documentation.xojo.com/index.php/TextEdit.Format

It is not about formatting the text, it is about validating the input.

Then Mask is probably the simplest. But since Steve is happy with his code, I will leave it at that.

Thanks guys. This works exactly as I wanted it to (in the LostFocus Event):

[code] dim b As Boolean = IsNumeric (Me.Text)

If NOT b then

MsgBox ("Please enter a numeric value.")

//Highlight input text to show where the issue is
Me.SelectAll

End If[/code]

BTW. The heading should have read: “Text field - restrict input to numeric data type”