Text field - restrict input to numeric data type

You know, Steve, you can change the title of a thread you created at any time. Click on it and edit away :slight_smile:

Well thanks Michel. I just changed it now. I wonder what the ramifications are.

Interesting that you can do that. Most forums don’t allow it.

Hmmmm . . . . I guess that means that if I was smart enough (which I’m not), I could contrive a scenario where people could put their views and ideas forward with me pretending to go along with it, then I could go back and change the title, and therefore completely change the context to suit myself.

Sort of like “Fake News” and “Alternative Facts”. Perhaps the zeitgeist of 2017 and beyond?

Actually you forgot me.SetFocus. It should be

[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.SetFocus
me.SelectAll

End If[/code]

But it also allows the following entries:

-0001.0.0
1.23.45.67

So no, your code is not working.

Ok thanks Markus. I’m not sure if I mentioned that I’m using 2016R3 and WIN7. Forum signatures would be very very useful in many cases, otherwise I should always precede my statements with 2016R3/WIN7 AND Never Likely to be developed for MAC-OS.

In my case, me.SetFocus is not necessary because after the fail, ie. NOT IsNumeric, the cursor defaults back to the originating text box and highlights the text (sort of auto SetFocus). That doesn’t mean to say that I won’t introduce SetFocus just to be sure.

Your sample test of entering -00001.0.0 OR 1.23.45.67 etc. DOES INDEED trigger “NOT IsNumeric” - which is EXACTLY what it should do.

I don’t see any issue - my code works perfectly :).

Actually, Steve, may I also suggest you post in the Windows channel to make things more obvious for possible replies. And also, you can change channel or a thread you started by clicking Controls in the upper right corner.

Actually, Markus is after edge cases. Val("1.23.45.67") produces the result 1.230000. It may not be what you want.

But I think at one point one is never entirely immune from stupid users…

[quote=311800:@Steve Kelepouris]Your sample test of entering -00001.0.0 OR 1.23.45.67 etc. DOES INDEED trigger “NOT IsNumeric” - which is EXACTLY what it should do.

I don’t see any issue - my code works perfectly :).[/quote]

Try it with commas instead of dots and see if your code catches it :wink:

(Difference between European and US/UK)

[quote=311802:@Michel Bujardet]Actually, Markus is after edge cases. Val("1.23.45.67") produces the result 1.230000. It may not be what you want.

But I think at one point one is never entirely immune from stupid users…[/quote]

In my experience users try to trip up your software :wink:

Your example is good. Yet, how should one interpret “1.23.45.67”. I tend to think what val does is fine, but the user may expect 12,345.67.

Talking about coma, “12,345.67” produces 12. And I know about the issue, since some users of CheckWriter tried to enter coma that way.

Thanks Michel for your suggestion.

I will post in the Windows channel in future if I think it’s ONLY Windows related. Unfortunately with my limited programming experience with Xojo, I’m unsure of the category if WIN or MAC-OS specific. I only know those things when I get there.

Even with my limited experience I do agree that the HARDEST part of programming is “User Error Detection”. I’ve spent many many hours with solutions I’ve created with other programming languages to ensure there are no surprises. ie. POLA Principle Of Least Astonishment.

Generally speaking, I’m pretty good at that part of programming.

Also, it’s important to point out that my software doesn’t require specific and crucial doses of medicine where one could die if the wrong dose was administered. I accept my limitations. At this point Steve Kelepouris shouldn’t be relied upon to deliver perfect software solutions.

Nevertheless, what Xojo has allowed me to do is create something that I never could have done before. For me it’s not about perfection, but a useful tool as a means to an end.

And I’m loving every minute of it :slight_smile:

[quote=311387:@Markus Winter]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][/quote]
Actually, this code lacks some valid keys (like the up/down arrow, which, on Mac, moves the insertion point to the beginning/end of text).

Therefore, the attached code I have provided would be a more effective solution . . . . . ? ? ?