How to limit the max length of typed user input

    Dec 02 2009 | 5:03 am
    One of the parameters I am managing in my patch has a text name that is a maximum of 20 characters long.
    There seems to be no way to limit the text input into a textedit object, nor a dialog object.
    Can anyone tell me how to pop up a box asking for user input, and limit the number of characters to a maximum of 20?

    • Dec 02 2009 | 5:06 am
      I was thinking that if all else fails I can do this via javascript, feeding it input from a key object. I just wondered if there was an existing way that my searching has failed to turn up, or if I'm missing something obvious!
    • Dec 02 2009 | 6:12 am
      Update: well, after a bit of fiddling around I've done it in Javascript.
      With a combination of:
      - Script gets fed ASCII characters with a [key] object
      - Bangs to toggle 'process/drop keypresses' (as [key] is always live)
      - Parameter to set the maximum number of characters allowed
      - Parameter to determine the destination of the string being output (I am using pattr to distribute these across my patch/subpatches)
      - Parameter to specify a filter to limit input to allowed characters
      ... it makes a pretty good general purpose keyboard handler.
      If there is an object in Max which can do this already, I'd be grateful for that info, I've had a good search around but couldn't find anything obvious!
    • Dec 23 2009 | 8:47 am
      You can atoi -> zl slice 20 -> itoa...
      I prefer Max over js or regexp. Its much more readable...
    • Dec 23 2009 | 11:18 am
      It may be too messy, but you could always just have a clickable keyboard of message boxes instead. A bit more work up front, but you can easily restrict the number of chars entered, as well as filter out illegal chars by simply not having them available.
    • Dec 23 2009 | 9:34 pm
      Here's one way. There's probably another way to split the string and count the length without resorting to [tosymbol], [fromsymbol] and javascript but it works.
    • Dec 23 2009 | 10:23 pm
      That's pretty nifty Luke, thanks. I've saved that in my 'useful things for next time' folder. My original solution, although it does use javascript, has worked very well in the current project but certainly this alternative is a lot more 'Max', and less 'scripty'.
      It also satisfies my main objective which was not to take a longer existing string and truncate it to N characters but to prevent more than N characters being entered in the first place.
      Thanks to everyone for picking this up.
    • Dec 27 2009 | 11:59 am
      Man, where the heck did [jstrigger] come from? Totally new to me...
      Interesting to see how the various combinations of commas and semicolons and backslashes are handled by various objects, like message boxes or sprintf, it can get kind of strange...but I guess that kind of thing is expected when you need to reserve characters for other purposes in some cases (like an expr object), but not in others (like a comment) still learning every day.
    • Dec 30 2009 | 12:44 am
      jstrigger comes from 4.5.6/4.5.7 I'm not sure anymore... It just uses JavaScript syntax within the parenthesis (because basically it executes the thing in JavaScript and the result is sent out the appropriate outlet).
    • Dec 31 2009 | 1:11 am
      OK - I realise this is done and solved, but I thought I'd see how few objects I could do this in. This is what I got, which should give the same behaviour as the patch above in case anyone is interested in a regexp approach:
    • Aug 10 2011 | 2:13 am
      Resurrecting an old post - Alex that's pretty slick with the regexp! But I have a need to limit the input to just ten *digits.* So I tried to modify the regexp to do so, and it works to limit the number of digits, but not to reject any alpha characters. What am I doing wrong?