How to limit the max length of typed user input

Eddy's icon

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?

Eddy

Eddy's icon

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!

Eddy

Eddy's icon

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!

Eddy

Tj Shredder's icon

You can atoi -> zl slice 20 -> itoa...

I prefer Max over js or regexp. Its much more readable...

Stefan

seejayjames's icon

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.

Luke Hall's icon

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.

Max Patch
Copy patch and select New From Clipboard in Max.

lh

Eddy's icon

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.

seejayjames's icon

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)...am still learning every day.

Emmanuel Jourdan's icon

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).

AlexHarker's icon
Max Patch
Copy patch and select New From Clipboard in Max.

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:

Alex

Dan Nigrin's icon
Max Patch
Copy patch and select New From Clipboard in Max.

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?

Rupert Pupkin's icon
Max Patch
Copy patch and select New From Clipboard in Max.

Resurrecting this old thread again! I'm trying to generate a 10-character ASCII string, and if the number of characters inputted into the textbox is < 10, then enough spaces (ASCII 32) to get up to 10 must be generated. (That part hasn't been created yet - still at the stage where I'm outputting the number of characters inputted vs remaining)

I've included the regex-based 10-character limit above, and the whole patch works fine as long as I don't go over 10. If I do, then things get a little weird and it stops counting properly. Same thing if I backspace below 0, but I found a workaround (not very elegant, but it works). Any ideas how to make this smoother?

Source Audio's icon

multiple ascii 32 will survive in max only if output as symbol,

like "12345 "

depends also where you want to insert that 10 chars, in textedit itself ?

here is one example, forces textedit to stay within 10 chars.

Max Patch
Copy patch and select New From Clipboard in Max.

Rupert Pupkin's icon

Ideally textedit itself wouldn't accept more than 10 characters (which is how the one above works), but the result should also always be 10 characters long (with spaces filling the rest if the text typed is < 10).

I added a few things to force the output to "32 32 32 32 32 32 32 32 32 32" whenever the text box is empty (this is for a voice edit SysEx, so a 10-character output is necessary).

The only thing missing perhaps for the changes to happen in real time. I added the regexp (.{10}) from the patch above and a bang trigger whenever the output changes (last outlet), but doing that I somehow lose the ability to have strings that begin with a space?

Max Patch
Copy patch and select New From Clipboard in Max.

Source Audio's icon

In that case it can be simplified a lot.

You also don't need to feed anything back to textedit,

and maybe filter chars that synth eventually does not accept ?

Rupert Pupkin's icon

Yeah filtering is definitely on the list but I'm a little stumped as to how, not that it's a priority. My synth accepts ASCII 0-127, but unless I'm mistaken the regexp object doesn't really work that way.

Source Audio's icon

regexp can well filter or pass only what one wants.

if all you want is small & capital letters and digits

regexp \\w+ would do.

or regexp [A-Z-a-z] for only letters

etc