7 Replies Latest reply on Jun 13, 2017 3:38 PM by philmodjunk

    Filter a number field entry to allow only a single period character

    DaleLong

      I have successfully designed a number entry field that allows only number characters and the "." character to be entered, thus mostly enforcing the entry of a valid number character. I am doing this in the following way.

       

      I use an OnObjectModify script trigger on the number field, which calls a 'Refresh Field' script. A filter value is passed into the script to allow the entry of certain characters - in this case:

       

      JSONFormatElements ( JSONSetElement ( "{}" ; "filter" ;  "0123456789."  ; JSONString ) )

       

      The script goes as follows:

       

      Set Variable [ $parameters ; Value: Get ( ScriptParameter ) ]

       

      If [ not IsEmpty ( $parameters ) ]

           If [ Left ( JSONFormatElements ( $parameters ) ; 1 ) = "?" ]

                Show Custom Dialog [ "Invalid JSON" ; "blahblah" ]

                Exit Script

           End If

       

           Set Variable [ $filter ; Value: JSONGetElement ( $parameters ; "filter" ) ]

       

           If [ $filter ]

                Set Field [ Filter ( Get ( ActiveFieldContents ) ; $filter ) ]

           End If

      End If

       

      Set Variable [ $current_selection_start ; Value: Get ( ActiveSelectionStart ) ]

      Set Variable [ $current_selection_size ; Value: Get ( ActiveSelectionSize ) ]

       

      Set Field [ Get ( ActiveFieldContents ) ]

       

      Set Selection [ Start Position: $current_selection_start ; End Position: $current_selection_size ]

       

       

      This works great, with the exception that it doesn't trap for the user entering multiple "." characters into the field. I have made another modification to this layout, basically making the 'Save' button disappear if the user enters more than one "." - this has the desired effect of preventing the entry of an invalid number value.

       

      However, it would be really slick if I could actively filter the field to prevent the entry of multiple "." characters, in the same way I'm enforcing only numeric and "." characters in the first place. Sadly, I haven't been able to figure out how to accomplish that. Anybody have any clever ideas how that might be accomplished?

        • 2. Re: Filter a number field entry to allow only a single period character
          DaleLong

          Well, I can't deny that this would work, so thanks for that! But this isn't quite achieving the user experience I was trying to go for - some of our data entry people wouldn't know what to do with the dialog options.

           

          I was hoping for something a bit less in-your-face than this...like I said, I am currently hiding the Save button from the user if they enter more than one ".", which prevents them from saving the value, so I believe it's in fairly good shape as it is. I just think it would be even more sublime if it simply didn't let you do the second "." entry at all.

          • 3. Re: Filter a number field entry to allow only a single period character
            siplus

            Just because there's JSON now there's no need to go Super Saiyan and visit the local grocery with a 18 wheeler. Things that have been serving us for decades are still there, ready to help.

             

            To go less in the face than this, but still in that dialog, after taking away the validation I suggested, return to the auto-enter option, click on calculated value and type the following:

             

            Substitute(Filter(Self; "0123456789."); "..";".")

             

            then take away the tick mark from "Do not replace existing value..." and that's it.

            1 of 1 people found this helpful
            • 4. Re: Filter a number field entry to allow only a single period character
              DaleLong

              Thanks siplus, that would address consecutive periods, but I don't think it would catch something like 2.3.5 for example. Nonetheless, this was helpful and did get me thinking in a direction to address this in the design pattern I was working toward. Much appreciated!

               

              For those interested, I achieved my desired result by including another parameter, "enforce_valid_number", a boolean set to true. Then there is an additional test in the script for this parameter, and if it is true, you set the active field to:

               

              Let (

                   [

                   ~value = Get ( ActiveFieldContents ) ;

                   ~two_periods = PatternCount ( ~value ; "." ) = 2 ;

                   ~position_one = Position ( ~value ; "." ; 1 ; 1 ) ;

                   ~position_two = Position ( ~value ; "." ; 1 ; 2 ) ;

                   ~length_middle = ~position_two - ~position_one + 1  ;

                   ~middle = Middle ( ~value ; ~position_one ; ~length_middle ) ;

                   ~middle_clean = Left ( ~middle ; ~length_middle - 1 )

               

                   ] ;

              If ( ~two_periods ; Substitute ( ~value ; ~middle ; ~middle_clean ) ; ~value  )

              )

               

               

              It's a bit code-heavy, but it only needs to be coded once, and performs exactly the way I expected, so I've got that going for me, which is nice! It also preserves the separate "filter" functionality should I want to disallow the entry of any other characters on any other field type.

               

               

               

              Regarding using JSON functions, it is as much for my own education that it appears in this script as it is anything else, as I intend to adopt its usage as my new standard for passing script parameters, as well as learn how to do some integrations with things outside of FileMaker. The best way to learn a thing is to use it often, there was a use case for a script parameter here, and, well, here I am I guess, shamelessly pulling up to the grocery store in my 18 wheeler

              • 5. Re: Filter a number field entry to allow only a single period character
                siplus

                If somebody enters 2.3.5 in a numeric field, the validation should be active and bring out a "you're fired" dialog.

                 

                Right in the face.

                 

                I'm all against protecting users by taking away responsibilities from them. It's a bad slope we're conceding here.

                 

                If all you are paid for is entering numbers, at least do it. Flawlessly 99% of the time. And don't do more than one error in the same input, otherwise we hire monkeys and pay bananas.

                 

                With all the data we have and clients stressing us for milliseconds, I can't afford fancy stylish things, unless they are faster - just faster - than what I have in place, working. Academic beauty hits the concrete with me.

                • 6. Re: Filter a number field entry to allow only a single period character
                  planteg

                  Another way to do it would be to check user entries at he types. For example use the script trigger OnObjectModify or OnObjectKeystroke. With the latter, you accept or reject each keystroke, but that may slow down things.

                   

                  Yet another way, you may use OnObjectValidate to prevent the user moving to another field if the contents is not a valid number.

                  1 of 1 people found this helpful
                  • 7. Re: Filter a number field entry to allow only a single period character
                    philmodjunk

                    Plus one on onobjectValidate.

                     

                    Best of both worlds is often to set up validation (or manage Security) limits on what the user can do to make sure that they can't do something you don't want them to do, then employ UI methods such as OnObjectValidate to handle the user errors in a manor more graceful than is possible with just the build in messages that pop up when validation or access privilege errors trip a system message.

                    1 of 1 people found this helpful