No announcement yet.

Elininating Punctuation Marks

  • Filter
  • Time
  • Show
Clear All
new posts

  • Elininating Punctuation Marks

    I want to write a snippet that forces the user to enter numbers
    and alphabets only. Is there an easy way to do this.

    ex. input "enter your last name ";lname$.

    If the user uses spaces or puncuation marks, the samll program I
    have written will crash.

    Any help will be appreciated. Thanks


  • #2
    Using Remove$ and ANY would be easy.



    • #3
      ValidString$ = "abcde...zABCDE..Z0123456789"
      IF VERIFY(userEntry$, ValidString$) = 0 THEN
        PRINT "All alphanumeric"
        PRINT "Non-alphanumeric characters found"
        Volts    = 500
        Duration = 4
        CALL SendVoltstoUser (Volts, Duration)
      END IF
      FWIW, this one is is easier in COBOL:
      * If not supported by your compiler create an ALPHANUMERIC classname in the SPECIAL-NAMES paragraph.


      [This message has been edited by Michael Mattias (edited September 18, 2003).]
      Michael Mattias
      Tal Systems (retired)
      Port Washington WI USA
      [email protected]


      • #4
        Maybe I can contribute here.

        We blocked this problem years ago in many places by using a commom
        SUB routine for text entry work. In that routine as part of the
        parameters passed to that SUB, you can pass prompts, format data,
        default 'entries', and so on. Notice those half quotes around the
        word <entries> in the previous sentence?

        One of the parameters passed to the SUB which is the common entry
        routine provides that the entry is scanned for the CHR$(34) in the
        text line the user has selected. If if finds one, it quietly and
        conveniently substitutes the apostrophe "'" for it instead!

        Over many years, in, for examply entries of names, where the convention
        seems to be to enclose a nickname in quote marks, substitution of the
        half quote instead has saved many a day for everyone. Obviously there
        are places where you couldn't do this, and accuracy is required even
        down to the correct CHR$(34) marks embedded in text! And, in this case,
        PowerBASIC will often let you pass them into string data by doing a
        string concantenation which includes the precise CHR$(34) as a string
        byte to be concantenated. Say "Y$ + CHR$(34) + Z$ + CHR$(34) + A$" is
        OK. But for the routine I/O work, the replacement technique for
        your keyboard I/O work for the user, may well get you an 'acceptable'
        substitute. The look and feel of the I/O may be good enough and it
        does not smash other things.

        Mike Luther
        [email protected]

        [This message has been edited by Mike Luther (edited September 18, 2003).]
        Mike Luther
        [email protected]


        • #5
          Dug up some old QB45 code that no only controls the input Numeric or Alpha Numeric but also controls the length of input here it is for what it's worth to you. The sub's are called like so:

          Enter lName$,30
          Here are the Sub's

          SUB edit (workstr$, worklen%)
           LET workstr$ = RTRIM$(workstr$)
           LET n = POS(0): PRINT STRING$(worklen%, "#"); : LOCATE , n
           PRINT workstr$;
          LET ky$ = INKEY$
           IF LEN(ky$) = 0 GOTO edit1
           IF ky$ = CHR$(13) THEN GOTO edit9
           IF ky$ = CHR$(8) THEN GOTO edit8
           IF ky$ = CHR$(59) THEN GOTO edit9
           IF ky$ < CHR$(27) OR ky$ > CHR$(127) THEN BEEP: GOTO edit1
           IF LEN(workstr$) >= worklen% THEN BEEP: GOTO edit1
           PRINT ky$;
           LET workstr$ = workstr$ + ky$
           GOTO edit1
            IF LEN(workstr$) < 1 THEN BEEP: GOTO edit1
            LOCATE , POS(0) - 1: PRINT "#"; : LOCATE , POS(0) - 1
            workstr$ = LEFT$(workstr$, LEN(workstr$) - 1)
            GOTO edit1
          END SUB
          SUB editn (workstr$, worklen%)
              ky$ = INKEY$
              IF ky$ = CHR$(27) THEN GOTO edita
              LET p = POS(0)
              LOCATE , p
              edit workstr$, worklen%
              work$ = RTRIM$(workstr$)
              IF LEN(work$) = 0 THEN GOTO edita
              FOR n = 1 TO LEN(workstr$)
              IF MID$(work$, n, 1) < "0" OR MID$(work$, n, 1) > "9" THEN GOTO reject
              GOTO edita
              workstr$ = work2$
              GOTO editn1
          END SUB
          SUB enter (workstr$, worklen%)
           LET workstr$ = ""
           edit workstr$, worklen%
          END SUB
          SUB entern (workstr$, worklen%)
           workstr$ = ""
           editn workstr$, worklen%
          END SUB
          Just throwing out OLD code


          You can either complain that rose bushes have thorns- or rejoice that thorn bushes have roses!
          There is a principle which is a bar against all information, which is proof against all arguments and which cannot fail to keep a man in everlasting ignorance - that principle is contempt prior to investigation.

          Herbert Spencer


          • #6
            Hi Keith,

            You'll need to put in a line or two of mods in fInput to exclude all
            characters out of your range but that shouln't be a big deal.

            [email protected]

            ' fInput% function for FirstBASIC
            '    coded by: Don Schullian  [email protected]
            '    public domain
            ' Welcome,
            '   This code is offered as an (better?) alternative to INPUT$. It
            ' allows for cursoring around a field, deletion, insertion, overwrite,
            ' scrolling, and bail out without saving the/any changes.
            '   Its use is demonstrated below using an array to allow for editing
            ' a screen full of data in one loop. This code could, very easily be
            ' placed into it's own function and called several times in a single
            ' program.
            '   There are a series of CONSTANTS set that are used by the function
            ' You may, of course, put their values into the function or, in some
            ' cases, send the values to the function to allow for more control by
            ' varied other functions.
            '   This offering is just a starting point for those of you who are
            ' more adventurous. Over the years I've developed 10 different
            ' variations of this function that control and guide the users' input.
            ' One of them works only for numerical input and looks & feels like
            ' a calculator; another allows input of only specific characters;
            ' while others handle hexadecimal input, masked fields, multiple lines
            ' and other varied field types. A bit of imagination goes a LONG way!
            '   fGetKey% is my basic keyboard input function and I never leave
            ' home without it. There is a full discussion on how and why on
            ' either of my web pages. [url=""][/url]  or
            ' [url=""][/url]  along with some other goodies.
            '   If you have any questions, give me a shout.
            ' Don
            ' fGetKey%(Datum$,Row%,Col%,VisCols%,MaxLen%,ExitKeys$)
            ' PURPOSE: Allow user input in an editable, friendly environment
            '  PARAMS: Datum$     incoming the data already found in the field
            '                     returning the edited data
            '          Row%, Col% the left most screen position of the field
            '          VisCols%   the number of visable characters on screen
            '          MaxLen%    the maximum number of characters in the field
            '          ExitKeys$  the MKI$(keyvalue%) of all the keys OTHER THAN
            '                       <ESC> and <ENTER> that will return from the
            '                       function
            '    NOTE: If VisCols% =< MaxLen% then the value will be set to match
            '          that of MaxLen%
            %EscKey   = &h001B             ' key codes returned by fGetKey%
            %EnterKey = &h000D
            %BkSpcKey = &h0008
            %DelKey   = &h5300
            %UpKey    = &h4800
            %DownKey  = &h5000
            %InsKey   = &h5200
            %LeftKey  = &h4B00
            %RightKey = &h4D00
            %CtrlDel  = &h9300
            %F10key   = &h4400
            %HomeKey  = &h4700
            %EndKey   = &h4F00
            %Fgrnd    = 15                 ' editing foreground
            %Bgrnd    =  1                 ' editing background
            '-------------------- start of test code
            COLOR 0, 7
            ' ================================================
            ' ======= this demonstrates the use for one field
            ' ================================================
            D$ = "Mary had a little lamb, its fleece was white as snow."
            G% = fInput%(D$, 2, 10, 20, 70, "")
            LOCATE 2, 1: PRINT D$;
            ' ==================================================
            ' ====== the following uses an array for 5 fields
            ' ==================================================
            DIM D$(5)
            DIM F%(2,5)
            RESTORE TestData
            FOR X% = 1 TO 5
              READ Prompt$
              READ D$(X%), F%(0,X%), F%(1,X%), F%(2,X%)
              Col% = F%(1,X%) - LEN(Prompt$) - 1
              COLOR 8, 7
              LOCATE F%(0,X%), Col%
              PRINT Prompt$;
              COLOR 1, 7
              LOCATE F%(0,X%), F%(1,X%)
              PRINT LEFT$(D$(X%), F%(2,X%))
            LOCATE 25, 1
            PRINT "Use Arrows to move - F-10 to save & exit - <ESC> to quit";
            ExitKeys$ = MKI$(%F10key) + MKI$(%UpKey) + MKI$(%DownKey)
            Fld% = 1
              IF Fld% < 1 THEN
                  Fld% = 5
                ELSEIF Fld% > 5 THEN
                  Fld% = 1
              END IF
              G% = fInput%( D$(Fld%), F%(0,Fld%), F%(1,Fld%), 0, F%(2,Fld%), ExitKeys$)
              SELECT CASE G%
                CASE %F10key : EXIT LOOP
                CASE %EscKey : EXIT LOOP
                CASE %UpKey  : DECR Fld%
                CASE ELSE    : INCR Fld%
              END SELECT
            FOR X% = 1 TO 5
              LOCATE X% + 10, 1
              PRINT D$(X%)
              DATA "Last Name:", "Schullian"   , 6, 20, 17
              DATA "Frst Name:", "Don"         , 7, 20, 17
              DATA "Street:"   , "My Street 27", 8, 20, 30
              DATA "City:"     , "Hometown"    , 9, 20, 30
              DATA "Zip:"      , "12345-2433"  , 9, 56, 10
            FUNCTION fGetKey%
              LOCAL G$
              LOCAL L%
                G$ = INKEY$
                L% = LEN(G$)
              LOOP UNTIL L% > 0
              IF L% = 1 THEN
                  fGetKey% = ASC(G$)
                  fGetKey% = CVI(G$)
              END IF
            END FUNCTION
            FUNCTION fInput% ( Datum$, Row%, Col%, VisLen%, MaxLen%, ExitKeys$ )
              LOCAL Bgrnd%                ' original background color
              LOCAL Cpos%                 ' current cursor position within string
              LOCAL E%                    ' temp variable
              LOCAL Exet$                 ' string vals of all exit keys
              LOCAL Fgrnd%                ' original foreground color
              LOCAL Inzert%               ' insert state
              LOCAL KeyVal%               ' incoming key-press value
              LOCAL MaxOff%               ' maximum offset position
              LOCAL Offset%               ' 1st character shown in field
              LOCAL Temp$$                ' working data string
              MAP Temp$$ * MaxLen%
              Temp$$  = LTRIM$(Datum$)
              Inzert% = 31
              Exet$   = MKI$(%EscKey) + MKI$(%EnterKey) + ExitKeys$
              Bgrnd%  = SCREEN(Row%, Col%, 1)
              Fgrnd%  = (Bgrnd% AND 15)
              Bgrnd%  = (Bgrnd%  \  16)
              Offset% = 1
              IF (VisLen% = 0) OR (VisLen% > MaxLen%) THEN VisLen% = MaxLen%
              MaxOff% = (MaxLen% - VisLen% + 1)
              GOSUB fInputEOL
              COLOR %Fgrnd, %Bgrnd
              LOCATE , , , Inzert%, 31
                IF Cpos% < 1 THEN
                    Cpos% = 1
                  ELSEIF Cpos% > MaxLen% THEN
                    Cpos% = MaxLen%
                END IF
                IF Cpos% < Offset% THEN
                    Offset% = Cpos%
                  ELSEIF (Cpos% - Offset% + 2) > VisLen% THEN
                    Offset% = (Cpos% - VisLen% + 1)
                    IF Offset% > MaxOff% THEN Offset% = MaxOff%
                END IF
                GOSUB fInputPrint
                LOCATE Row%, (Col% + Cpos% - Offset%), 1
                KeyVal% = fGetKey%
                E% = INSTR(Exet$, MKI$(KeyVal%))
                IF (E% AND 1) = 1 THEN EXIT LOOP
                SELECT CASE KeyVal%
                  CASE 32 TO 255 : IF (Inzert% = 0) OR (Cpos% = MaxLen%) THEN
                                       MID$(Temp$$, Cpos%, 1) = CHR$(KeyVal%)
                                       Temp$$ = LEFT$(Temp$$, Cpos% - 1) + _
                                                 CHR$(KeyVal%)           + _
                                                 MID$(Temp$$, Cpos%)
                                   END IF
                                   INCR Cpos%
                  CASE %BkSpcKey : IF Cpos% > 1 THEN
                                     DECR Cpos%
                                     GOSUB fInputStrip
                                   END IF
                  CASE %DelKey   : GOSUB fInputStrip
                  CASE %HomeKey  : Cpos% = 1
                  CASE %EndKey   : GOSUB fInputEOL
                  CASE %LeftKey  : DECR Cpos%
                  CASE %RightKey : INCR Cpos%
                  CASE %InsKey   : Inzert% = ( Inzert% XOR 31 )
                                   LOCATE , , , Inzert%, 31
                END SELECT
              Offset% = 1
              IF KeyVal% <> %EscKey THEN
                  Temp$$ = LTRIM$(Temp$$)
                  Datum$ = RTRIM$(Temp$$)
                  Temp$$ = Datum$
              END IF
              COLOR Fgrnd%, Bgrnd%
              GOSUB fInputPrint
              fInput% = KeyVal%
              EXIT FUNCTION
              '------------- local routines
              LOCATE Row%, Col%, 0
              PRINT MID$(Temp$$, Offset%, VisLen%);
              Temp$$ = LEFT$(Temp$$, Cpos% - 1) + MID$(Temp$$, Cpos% + 1)
              FOR Cpos% = MaxLen% TO 1 STEP -1
                IF ASCii(MID$(Temp$$, Cpos%)) <> 32 THEN EXIT FOR
              INCR Cpos%
            END FUNCTION



            • #7
              Mis-posted, removed and re-posted to correct forum

              [This message has been edited by Richard Angell (edited March 20, 2007).]
              Rick Angell


              • #8
                This is a trivial problem if you process the information provided
                after the input, but requires your own custom routine to handle
                input if you want to block certain characters from being entered
                from the keyboard,

                Let's focus on the trivial way. Say your input is in aa$, and
                you only want to allow numeric data. Just do aa$=RETAIN$(aa$,
                ANY "0123456789"), and all you have left is the digits. If you
                want to allow a dollar sign and decimal place, just add them to
                what you will accept: aa$=RETAIN%(aa$,ANY "$.0123456789").

                We can determine that capital "A" corresponds to a decimal 65, and
                that a capital "Z" corresponds to a decimal 90. At the same time,
                a zero ("0") has a code value of 48, and a "9" has the code value
                of 57. The small "a" and "z" have the code values of 97 and 123
                respectively. We can use this information with a CHR$()
                function to create our own custom mask that will only accept and
                use upper case letters and digits like this:
                  REPLACE ANY CHR$(97 TO 123) WITH CHR$(65 TO 90) IN AA$
                  aa$=RETAIN$(aa$, ANY CHR$(65 TO 90,48 TO 57))
                IF retaining original position within aa$ is inportant, than
                replacing unwanted characters with something innocous like
                spaces might be better. This is a great job for REPLACE.
                  ff$=CHR$(0 TO 255)                    'cover every ASCII code
                  ff$=REMOVE$(ff$, ANY CHR$(65 TO 90, 48 TO 57)) 'remove valid chars
                  REPLACE ANT ff$ WITH SPACE$(LEN(ff$)) IN aa$ 'replace the rest with spaces
                Like I said. Trivial, thanks to the many flexable string
                commands provided with PowerBasic.

                Oops! I forgot which board I was writing to at the time. PB/DOS
                lacks some of the string commands found in PB/CC and PB/Win. So I
                guess this post merely reveals some of the rewards you get when you
                decide to move up to a later compiler.

                Old Navy Chief, Systems Engineer, Systems Analyst, now semi-retired

                [This message has been edited by Donald Darden (edited March 21, 2007).]


                • #9
                  Michael - can I have a listing of SendVoltsToUser ? I've been looking for something like that for years.