WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Dialog Editor version 6.X
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Checking Input Char by Char in Dialogs

Keywords:     isnumber ischaracter   ip address

Question:

I am looking for a way or a script to help me write pre-formatted dialog boxes. What i mean is we have a requirement to build a small application to get phone numbers in a format like "(514) 832-7000" or a TCP/IP address such as "192.168.0.1".

I would like to know if there is an easy way to get an entry field where the user would input information that would be automatically validated as the user is entering information ie. the user would not be able to enter letters where numbers are required and vice-versa !!!

Please let me know if anyone would have a solution for this problem.

Answer:

The new Dialogs with callbacks make this fairly easy. If you have instructed the Dialog to watch the edit boxes using DialogProcOptions each keystroke in an editbox triggers the callback. In this case this is helpful. For the phone number you can set the initial value to "(" (or set a default area code) then with each keystroke insure that it is a number. Once the Length of the value in the editbox hits 4 you could automatically enter the closing ")". You can then fill out the rest of the field in a similar manner filling in the dashes automatically and enforcing numbers only. The IsNumber function is one way to accomplish this limitation.

Here is some code I threw together which *attempts* to validate an IP Address...

#definefunction MainWnd(dlghandle,dlgmessage,dlgID,reserved4,reserved5)

   ;Define a few handy constants here
   MSG_Initialization=0
   MSG_EDITBOX=5
        DCTL_EditBox = 3

   switch dlgmessage

       case MSG_Initialization  ; The Dialog Initialization call
          dialogprocoptions(dlghandle,MSG_EDITBOX,@true)
          break
               
                 case MSG_EDITBOX
                   ebval=dialogcontrolget(dlghandle,dlgID,DCTL_EditBox)

                        
                        ;Check string either integer or (.)
                        check = CheckIPAddress(ebval)
                        if check == @False
                           message("NOTE","Not a valid ip address")
                        endif
         break
   endswitch

   return (-1) ; do default processing
#endfunction

#DefineFunction CheckIPAddress(szAddr)
	;Check string is no longer than 15 characters
	if StrLen(szAddr)>15
	  ;Message("NOTE","Not a valid ip address")
	  Return @False
	endif
	
	;Check there are 4 octets in the string
	OctetCount = ItemCount(szAddr, '.')
	if OctetCount <> 4
	  ;Message('Error', 'Malformed IP address')
	  Return @False
	endif

	for i = 1 to 4
	  Octet = ItemExtract(i, szAddr, '.')
	  if Octet < 0 || Octet > 255 || !IsNumber(Octet)
	    ;Message('Not Valid IP Address', StrCat('Octet #', i, ' is not numeric between 0 and 255')
		 Return @False
	  endIf
	next 

   Return @true
#EndFunction

DlgFormat=`WWWDLGED,6.1`
DlgCaption=`Validate Text`
DlgX=002
DlgY=050
DlgWidth=225
DlgHeight=121
DlgNumControls=004
DlgProcedure=`MainWnd`
DlgFont=`DEFAULT`
DlgTextColor=`DEFAULT`
DlgBackground=`DEFAULT,DEFAULT`

Dlg001=`057,082,033,011,PUSHBUTTON,DEFAULT,"OK",1,1,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
Dlg002=`111,082,033,011,PUSHBUTTON,DEFAULT,"Cancel",0,2,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
Dlg003=`042,047,117,011,EDITBOX,IPaddr,"",DEFAULT,3,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
Dlg004=`041,033,127,011,STATICTEXT,DEFAULT,"Please enter an IP Address in the form ###.###.###",DEFAULT,4,DEFAULT,DEFAULT,DEFAULT,DEFAULT`

ButtonPushed=Dialog("Dlg")

;Check string either integer or (.)
check = CheckIPAddress(IPaddr)
if check == @False
   message("NOTE","Not a valid ip address")
endif
You would have to modify the code to fit your exact needs....
Article ID:   W15125
File Created: 2004:03:18:10:46:02
Last Updated: 2004:03:18:10:46:02