Block msEdit input if not numeric?

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
User avatar
Rain
Automation Wizard
Posts: 550
Joined: Tue Aug 09, 2005 5:02 pm
Contact:

Block msEdit input if not numeric?

Post by Rain » Tue Jan 16, 2007 4:34 pm

Is there an easy way to block msEdit inputs that are not numeric?

I could use the StringReplace command but that would mean I have to do it for all letters, special characters and foreign characters. I was hoping there is an easier way to block those inputs.

Any suggestion?

Thank you,

Rain

User avatar
JRL
Automation Wizard
Posts: 3504
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Tue Jan 16, 2007 7:01 pm

Hi Rain,

This isn't perfect but it was the best I could over a lunch hour. :)
The failures I know about are: Fixed
1) Spaces at the end of the text
2) A "-" placed at the beginning of the text after a number has been entered.

Edit...
Fixed 1 and 2 and also found that "e" was a problem... fixed that also.

Code: Select all

Dialog>Dialog1
   Caption=Dialog1
   Width=190
   Height=144
   Top=130
   Left=59
   Edit=msEdit1,32,16,121,
   Button=Ok,56,56,75,25,3
EndDialog>Dialog1

Let>previous=
Show>dialog1
Label>start
GetDialogAction>dialog1,r1
If>r1=2,exit
if>r1=3,process
If>%dialog1.msedit1%<GoSub>test
EndIF
Wait>0.01
Goto>start
Label>exit

SRT>process
//Do stuff
END>process

SRT>test
Separate>%dialog1.msedit1%,-,var
If>%var_count%>1
  MDL>Numeric Input only...
  Let>dialog1.msedit1=%previous%
  ResetDialogAction>dialog1
  Press End
EndIF

Separate>%dialog1.msedit1%,%SPACE%,var
If>%var_count%>1
  MDL>Numeric Input only...
  Let>dialog1.msedit1=%previous%
  ResetDialogAction>dialog1
  Press End
EndIF

Separate>%dialog1.msedit1%,E,var
If>%var_count%>1
  MDL>Numeric Input only...
  Let>dialog1.msedit1=%previous%
  ResetDialogAction>dialog1
  Press End
EndIF

Separate>%dialog1.msedit1%,e,var
If>%var_count%>1
  MDL>Numeric Input only...
  Let>dialog1.msedit1=%previous%
  ResetDialogAction>dialog1
  Press End
EndIF

Let>test_res=%dialog1.msedit1%/1
Separate>%test_res%,/,var
If>%var_count%>1
  MDL>Numeric Input only...
  Let>dialog1.msedit1=%previous%
  ResetDialogAction>dialog1
  Press End
Else
  Let>Previous=%dialog1.msedit1%
EndIf
Label>test_complete
END>test

User avatar
pgriffin
Automation Wizard
Posts: 460
Joined: Wed Apr 06, 2005 5:56 pm
Location: US and Europe

Post by pgriffin » Tue Jan 16, 2007 8:00 pm

here is a snippet of code I use.

vbe>IsNumeric("%Dialog.msEdit%"),IsNumResult
if>%IsNumResult%=False
mdl>Field must be numeric
rda>Dialog
goto>DialogLoop
endif>


Of course, this code ensures the field IS numeric, so adjust the code to suit your needs.

Hope I didn't oversimplify your problem.

User avatar
Rain
Automation Wizard
Posts: 550
Joined: Tue Aug 09, 2005 5:02 pm
Contact:

Post by Rain » Wed Jan 17, 2007 1:50 pm

Thank you JRL for spending your lunch hour to help me with my problem, your script does exactly what I want. I really appreciate it! I’ve played around with the one you posted before you edited it and got it to work. But now that I see SkunkWorks little 6 liner I have to go with his script.
SkunkWorks wrote:Hope I didn't oversimplify your problem.
Showoff! [Just kidding]
Thank you for taking the time to post your script, it works like charm.

User avatar
JRL
Automation Wizard
Posts: 3504
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Wed Jan 17, 2007 11:41 pm

Rain and anyone who needs this ability,

Before writing the script I posted (above), I looked for the VBscript that SkunkWorks posted because I knew I'd seen it on the forum. However I couldn't find it. Instead I found the "divide by one" test that was posted by Bob Hansen. Wasn't my first choice but it seemed to be workable. Now that I've tested both methods I prefer Bob's.

I set up the VBscript "IsNumeric" in a similar fashion as my earlier script so that it could be tested. Following are the results.

IsNumeric allows the following characters:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, e, d, one decimal point "." and comma

"e" and "d" can be entered by typing a few numeric characters then arrowing left a character or 2 (but no more than 3). Entering "d" doesn't seem to have any adverse effect other than you have a "d" in your number. entering "e" more than 2 characters back will pop a Macro Scheduler "floating point overflow error". After that error Macro Scheduler needs to be shut down and restarted for scripts to function properly.

Additionally if a quote is entered, a VBscript error pops up and the script crashes.

I think the VBscript could be made error free with some additional character testing similar to what is in my script, but I haven't yet tried to do that.

My script using Bob's "divide by one" test allows the following characters:
0, 1, 2, 3, 4, 5, 6, 7, 8 and 9


The "IsNumeric" test script:

Code: Select all

Dialog>Dialog1
   Caption=Dialog1
   Width=190
   Height=144
   Top=130
   Left=59
   Edit=msEdit1,32,16,121,
   Button=Ok,56,56,75,25,3
EndDialog>Dialog1
vbstart
vbend
Let>previous=
Show>dialog1
Label>start
GetDialogAction>dialog1,r1
If>r1=2,exit
if>r1=3,process
If>%dialog1.msedit1%<GoSub>test
EndIF
Wait>0.01
Goto>start
Label>exit

SRT>test
vbe>IsNumeric("%Dialog1.msEdit1%"),IsNumResult
if>%IsNumResult%=False
  MDL>Numeric Input only...
  Let>dialog1.msedit1=%previous%
  ResetDialogAction>dialog1
  Press End
Else
  Let>Previous=%dialog1.msedit1%
endif>
END>test

User avatar
pgriffin
Automation Wizard
Posts: 460
Joined: Wed Apr 06, 2005 5:56 pm
Location: US and Europe

Post by pgriffin » Thu Jan 18, 2007 4:53 am

Dick,

You give up on VBScript too soon. There is plenty of functionality there to get around the issues you found. Int, Fix, IsChar, etc....

You mention the "Divide by one " method, but I don't see any division in your example. Maybe part of the code was truncated?

it is also possible to test each character from an edit box, character by character to see that they are found in a "Valid" string. If you just would rather stay only in MacroScript, try this....

let>SomeEdit=12#. e9. d"
let>ValidNbr=0123456789
let>ValidString=
len>SomeEdit,lenEdit
let>cPos=0
repeat>cPos
let>cPos=cPos+1
midstr>%SomeEdit%,%cPos%,1,chkChar
Pos>%chkChar%,%ValidNbr%,1,vChar
if>vChar>0
concat>ValidString,chkChar
endif>
Until>cPos=lenEdit
mdl>%ValidString%

Keep scripting....SW

User avatar
JRL
Automation Wizard
Posts: 3504
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Thu Jan 18, 2007 5:36 am

Paul,

I haven't given up on VB, someday I intend to try to learn and use it :)
I'll look at your additions tomorrrow and see if I can comprehend any of it.
I don't see any division in your example.
Its almost invisible, the line is in the first sample script:
Let>test_res=%dialog1.msedit1%/1

Also, I pointed out that there could be additional character testing to make the VBscript work. My main concern was/is that by itself, "IsNumeric" has some problems.

Thanks for the additional info.
Dick

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Thu Jan 18, 2007 6:12 am

I don't have access to Macro Scheduler right now, but divide by 1 should be using mod to look at the remainder. Divide value by 1. if the value was text, you will get an error. If there is a remainder, then value is a fraction, not an integer.

Should look something like this:

Code: Select all

Let>remainder={%dialog1.msedit1% mod 1}
If>%remainder%=error,NotInteger
If>%remainder%<>0,NotInteger         

MessageModal>Value is an integer
Goto>End

Label>NotInteger
MessageModal>Value is not an integer.  Please renter

Label>End
As mentioned above, I don't have access to Macro Scheduler right now, so this is untested, and may have syntax errors. Value of "error" is probably wrong, step through and look at watch list. Syntax of "mod" line may also be incorrect.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Fri Jan 19, 2007 11:24 am

VBscript's IsNumber function looks to see if the expression can be interpreted as a number. Numbers can be integers, doubles or money types and can therefore contain the decimal place holder, E for exponentials and I think the D either means Double or is used in place of E in some countries.

So, we need to expand on IsNumber a little if we don't want those Ds and Es. And we need a new function for IsInteger. Here's my solution:

Code: Select all

VBSTART
Function IsNumber(var)
  IsNumber = (LCase(var) = UCase(var)) and isNumeric(var)
End Function

Function IsInteger(var)
  IsInteger = IsNumber(var) and (InStr(var,".") = 0)
End Function
VBEND

//Examples - step thru with debugger to see results ...
VBEval>IsNumber("254"),IsNum
VBEval>IsNumber("254.32"),IsNum
VBEval>IsNumber("2d4E"),IsNum
VBEval>IsInteger("254"),IsInt
VBEval>IsInteger("254.33"),IsInt
VBEval>IsInteger(34),IsInt
VBEval>IsInteger(34.2),IsInt
This gives you a new function called IsNumber which does the same as IsNumeric but ignores Ds and Es. It also gives you IsInteger which uses IsNumber and looks for the period.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts