6 Replies Latest reply on May 5, 2009 2:59 PM by mrvodka

    Counting Characters in a field

    rsagall_1

      Title

      Counting Characters in a field

      Post

      I need to count the number of characters in a field as data is entered. I can use the "Length" function with a Script Trigger and all works fine until I hit the delete key. Rather than the count going down by one each time I hit the delete key, the count goes up by one. When I type another character the count become correct. How can I fix this?

       

      A second question -  Is there a way to block additional characters being typed into the field once a certain number is reach?

       

      Thanks,

       

      Rich

       

        • 1. Re: Counting Characters in a field
          philmodjunk
            

          On the Validation tab of a field's Options dialog, there's a Maximum number of characters setting. If you clear the "Allow user to override during data entry" option, you will keep all users from entering more characters than the limit you define.  To bring up the Field Option dialog, go to Manage | Database | Fields and double click the field in the field definitions list.

           

          If you need to display a character count on a field. Set up a calculation field

           

          Length(FieldName)

           

          Where "FieldName" is the name of the field you want a character count. You may need to set the field's storage option to unstored if it doesn't update promptly.

          • 2. Re: Counting Characters in a field
            KIDO
              

            Hi Rich

             

            I believe to have an answer to both of your questions. You mus be using OnKeyStroke script trigger which runs after the keystroke. Try the following:

             

                 - Deselect your script trigger in the txt input field.

                 - Rewrite your script as follows (making the necessary replacement):

             <!-- [if gte mso 9]><xml> <w:WordDocument>   <w:View>Normal</w:View>   <w:Zoom>0</w:Zoom>   <w:PunctuationKerning/>   <w:ValidateAgainstSchemas/>   <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>   <w:IgnoreMixedContent>false</w:IgnoreMixedContent>   <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>   <w:Compatibility>    <w:BreakWrappedTables/>    <w:SnapToGridInCell/>    <w:WrapTextWithPunct/>    <w:UseAsianBreakRules/>    <w:DontGrowAutofit/>   </w:Compatibility>   <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif] --><!-- [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif] --><!-- /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal      {mso-style-parent:"";      margin:0cm;      margin-bottom:.0001pt;      mso-pagination:widow-orphan;      font-size:12.0pt;      font-family:"Times New Roman";      mso-fareast-font-family:"Times New Roman";      mso-ansi-language:EN-GB;} @page Section1      {size:595.3pt 841.9pt;      margin:72.0pt 90.0pt 72.0pt 90.0pt;      mso-header-margin:35.4pt;      mso-footer-margin:35.4pt;      mso-paper-source:0;} div.Section1      {page:Section1;} --><!-- [if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable      {mso-style-name:"Table Normal";      mso-tstyle-rowband-size:0;      mso-tstyle-colband-size:0;      mso-style-noshow:yes;      mso-style-parent:"";      mso-padding-alt:0cm 5.4pt 0cm 5.4pt;      mso-para-margin:0cm;      mso-para-margin-bottom:.0001pt;      mso-pagination:widow-orphan;      font-size:10.0pt;      font-family:"Times New Roman";      mso-ansi-language:#0400;      mso-fareast-language:#0400;      mso-bidi-language:#0400;} </style> <![endif] -->

                   Script  "CharCount" (limited to 5 chars: - replace 5 with the number of chars desired)

             

                                    Set Field [ YourCharCount_NumberField;  Length ( Left (YourTextField ; 5 ) ) ]

                                     If[ Length (YourTextField) > 5]

                                          Beep

                                          Show Custom Dialog [ “Limit exceeded…….”; “Only 5 characters allowed!”]

                                          Set Field [YourTextField ; Left (YourTextField ; 5 )]

                                     End If

                 

                   - In your input text field Script Trigges select OnObjectModify and assign your new script nname ( "CharCount" in this example)

             

            It works lovely!

             

            KIDO

             

            • 3. Re: Counting Characters in a field
              rsagall_1
                

              Kido - thanks for the suggestion. It comes close.

               

              I deleted the Left command because that caused a problem. I also had to add "+1" to get an accurate count. Now it works fine until I hit the "Delete" key. The counter includes that in the count. If I then enter a character into the field the count returns to normal.

               

              It's minor bug but I would like to get it taken care of. Any suggestions?

               

              Rich

               

              • 4. Re: Counting Characters in a field
                philmodjunk
                  

                Change the first line of the script to:

                 

                Set Field [ YourCharCount_NumberField;  YourCharCount_NumberField + If (code ( Get(TriggerKeystroke)) = 8 ; 0 ; 1 )]

                 

                8 is the code for the backspace key.

                • 5. Re: Counting Characters in a field
                  KIDO
                     <!-- [if gte mso 9]><xml> <w:WordDocument>   <w:View>Normal</w:View>   <w:Zoom>0</w:Zoom>   <w:PunctuationKerning/>   <w:ValidateAgainstSchemas/>   <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>   <w:IgnoreMixedContent>false</w:IgnoreMixedContent>   <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>   <w:Compatibility>    <w:BreakWrappedTables/>    <w:SnapToGridInCell/>    <w:WrapTextWithPunct/>    <w:UseAsianBreakRules/>    <w:DontGrowAutofit/>   </w:Compatibility>   <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif] --><!-- [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif] --><!-- /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal      {mso-style-parent:"";      margin:0cm;      margin-bottom:.0001pt;      mso-pagination:widow-orphan;      font-size:12.0pt;      font-family:"Times New Roman";      mso-fareast-font-family:"Times New Roman";      mso-ansi-language:EN-GB;} @page Section1      {size:595.3pt 841.9pt;      margin:72.0pt 90.0pt 72.0pt 90.0pt;      mso-header-margin:35.4pt;      mso-footer-margin:35.4pt;      mso-paper-source:0;} div.Section1      {page:Section1;} --><!-- [if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable      {mso-style-name:"Table Normal";      mso-tstyle-rowband-size:0;      mso-tstyle-colband-size:0;      mso-style-noshow:yes;      mso-style-parent:"";      mso-padding-alt:0cm 5.4pt 0cm 5.4pt;      mso-para-margin:0cm;      mso-para-margin-bottom:.0001pt;      mso-pagination:widow-orphan;      font-size:10.0pt;      font-family:"Times New Roman";      mso-ansi-language:#0400;      mso-fareast-language:#0400;      mso-bidi-language:#0400;} </style> <![endif] -->

                  Hi Rich

                   

                  I have reviewed and tested the script and can’t see why it doesn’t work with you. The only difference is that I use a number field to display the result, while you use a counter. I have tested with the delete and backspace keys in different positions and still get the correct results. Let’s recap: - For testing purpose use a number field to display the results:- YourCharCount_NumberField in the example.

                   

                  Step 1 – Create a new script as follows, minding the replacements.

                   

                  Script  "CharCount" (limited to 5 chars: - replace 5 with the number of chars desired)

                                           Set Field [ YourCharCount_NumberField;  Length ( Left (YourTextField ; 5 ) ) ]

                                          If[ Length (YourTextField) > 5]

                                                Beep

                                                Show Custom Dialog [ “Limit exceeded…….”; “Only 5 characters allowed!”]

                                                Set Field [YourTextField ; Left (YourTextField ; 5 )]

                                           End If

                  Step 2 – In Layout mode select your text field and then MENU – FORMAT – SET SCRIPT TRIGGERS… -  check the box OnObjectModify and assign the new CharCount script. Don’t forget to deselect other script trigger options, (if any).

                   

                  And that’s it. It works with FM 10 on a Windows XP.

                   

                  Best regards

                   

                  KIDO

                   

                  • 6. Re: Counting Characters in a field
                    mrvodka
                      

                    A couple of techniques have already been posted on here but here is one more.

                     

                    Let's call your fields: YourField and LengthYourField.

                     

                    Script #1: ( Used on YourField w/ OnObjectKeystroke )

                     

                    Set Variable [ $keypress; Value:Code ( Get ( TriggerKeystroke ) ) ]
                    Set Variable [ $n; Value:Length ( INV::TaxRate ) ]
                    Set Variable [ $limit; Value:5 ]

                    #
                    If [ $n ≥ $limit and not Get(ActiveSelectionSize) ]
                       If [ $keypress ≠ 8 ]
                          Show Custom Dialog [ Message: "Over 5"; Buttons: “OK” ]
                          Exit Script [ Result: 0 ]
                       End If
                    End If

                     

                     

                     

                    Script #2: ( Used on YourField w/ OnObjectModify )

                     

                    Set Field [ LengthYourField; Length ( YourField ) ]

                     

                     

                     

                    Both the OnObjectKeystroke and OnObjectModify script triggers should be put on the field that you enter data "YourField"

                     

                     

                    Good luck.