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 11 characters if strlen(szAddr)>11 message("NOTE","Not a valid ip address") endif ;Check format nnn.nnn.nnn For x = 1 to StrLen(szAddr) char = StrSub(szAddr,x,1) switch (x) case 1 case 2 case 3 case 5 case 6 case 7 case 9 case 10 case 11 if IsNumber(char)!= @True Return @False endif break; case 4 case 8 if char != "." Return @False endif break; ENDSWITCH 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") endifYou would have to modify the code to fit your exact needs....
Article ID: W15125