Search code examples
pythonraw-input

Limit raw_input length


I'm looking for a solution to limit the number of characters a user can type in a raw_input. I have two raw_inputs in a same line, that looks like this:

Name: (user input)                           Date of Birth: (user input)

If a user types in a name that is bigger than X amount of characters, it will start writing on top of Date of Birth, like this:

Name: Mary Jane Smith McDonald Obama Romney Bushh of Birth: (user input)

The closest thing I found was this: Limiting Python input strings to certain characters and lengths, but that wouldn't work for me. I don't want it to show a warning; I want it to simply stop accepting more characters and/or go to the next input.

I'm using blessed, which is a library that uses curses to position the second input in the same line as the first.

Any ideas?


Solution

  • The function getstr() which is the raw_input alternative for curses, works with a character constraint:

    s = stdscr.getstr(0,0, 15) #Input is limited to 15 characters, once the limit is hit, it stops accepting more