# ControlP5, How to set the maximum number of characters in a Text Field?

**URL:** <https://discourse.processing.org/t/controlp5-how-to-set-the-maximum-number-of-characters-in-a-text-field/32062>\
**Category:** Libraries\
**Created:** [September 2, 2021, 4:40pm UTC](https://discourse.processing.org/t/controlp5-how-to-set-the-maximum-number-of-characters-in-a-text-field/32062 "2021-09-02T16:40:49Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jhsa](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@jhsa](https://discourse.processing.org/u/jhsa)\
**Post date:** [September 2, 2021, 4:40pm UTC](https://discourse.processing.org/t/controlp5-how-to-set-the-maximum-number-of-characters-in-a-text-field/32062/1 "2021-09-02T16:40:49Z")

</div>

I would like to allow a user to insert a maximum of 10 characters in the text field…  
Been looking and cannot find any option for that. Researched online, found nothing.  
Thank you for your help.

---

<div class="post-metadata">

**Author:** ![MiguelSanches](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/miguelsanches/32/11172_2.png) [@MiguelSanches](https://discourse.processing.org/u/MiguelSanches)\
**Post date:** [September 3, 2021, 12:51pm UTC](https://discourse.processing.org/t/controlp5-how-to-set-the-maximum-number-of-characters-in-a-text-field/32062/2 "2021-09-03T12:51:25Z")

</div>

Hi @jhsa

Looking into the documentation I didn’t find any way to limit the number of input characters in the text field. However, I found the following discussion, which you could also come across:

[https://forum.processing.org/two/discussion/11864/textfield-in-cp5.html](https://forum.processing.org/two/discussion/11864/textfield-in-cp5.html)

Following the discussion I implemented the following code, check if it works!

```auto
import controlP5.*;

ControlP5 cp5;

String textValue = "";

void setup() {
  size(700,400);
  
  PFont font = createFont("arial",20);
  
  cp5 = new ControlP5(this);
  
  cp5.addTextfield("input")
     .setPosition(20,100)
     .setSize(200,40)
     .setFont(font)
     .setFocus(true)
     .setColor(color(255,0,0))
     ;
  textFont(font);
}

void draw() {
  background(0);
  fill(255);
  text(cp5.get(Textfield.class,"input").getText(), 360,130);
  text(textValue, 360,180);
  
  text("char counter = " + count, 370,170);
}

int count = 0;
void keyTyped(){
  if(cp5.get(Textfield.class,"input").isActive()){
    if(key == BACKSPACE || key == DELETE){
      count--;
      
    } else count ++;
    if(count < 0) count = 0;
    
    if(count > 10){
      String textString = cp5.get(Textfield.class,"input").getText();
      cp5.get(Textfield.class,"input").setText(textString.substring(0,10));
      count--;
    }
  }
}

void controlEvent(ControlEvent theEvent) {
  if(theEvent.isAssignableFrom(Textfield.class)) {
    println("controlEvent: accessing a string from controller '"
            +theEvent.getName()+"': "
            +theEvent.getStringValue()
            );
  }
}

public void input(String theText) {
  // automatically receives results from controller input
  println("a textfield event for controller 'input' : "+theText);
}

```

Best regards,

---

<div class="post-metadata">

**Author:** ![jhsa](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@jhsa](https://discourse.processing.org/u/jhsa)\
**Post date:** [September 4, 2021, 5:10pm UTC](https://discourse.processing.org/t/controlp5-how-to-set-the-maximum-number-of-characters-in-a-text-field/32062/3 "2021-09-04T17:10:30Z")

</div>

Hi,Thank you. It works, but it gives an error and crashes if I hit enter and try to write again.

---

<div class="post-metadata">

**Author:** ![jhsa](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@jhsa](https://discourse.processing.org/u/jhsa)\
**Post date:** [September 4, 2021, 8:22pm UTC](https://discourse.processing.org/t/controlp5-how-to-set-the-maximum-number-of-characters-in-a-text-field/32062/4 "2021-09-04T20:22:25Z")

</div>

If I reset the counter to zero as shown below, I can hit enter without errors, but if I keep typing and hitting Enter, I will still have the error at some point, although not every time I do it.  
The error is:  
“StringIndexOutOfBoundsException:”

```auto
public void input(String theText) {
  // automatically receives results from controller input
  println("a textfield event for controller 'input' : "+theText);
  count = 0;
}

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [September 4, 2021, 10:57pm UTC](https://discourse.processing.org/t/controlp5-how-to-set-the-maximum-number-of-characters-in-a-text-field/32062/5 "2021-09-04T22:57:13Z")

</div>

@MiguelSanches

Thanks!

This will work nicely for a project I am working on.

I got it working for the maximum positive integer input:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/e/eb6d4fcaebbf19c8511bcda8654ac02c66473031.png)

My final version has lots of error checking for my needs.  
I just shared the bare minimum to show my approach to this.

Before “Enter”:

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/f1e84cdb4c79df364ae7bce00b3f6053a28b131b.png)

After “Enter”:

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/e/eab50ba3d76f1201e9e75610a4f9d8ff8b8fa344.png)

`:)`

---

<div class="post-metadata">

**Author:** ![MiguelSanches](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/miguelsanches/32/11172_2.png) [@MiguelSanches](https://discourse.processing.org/u/MiguelSanches)\
**Post date:** [September 6, 2021, 8:06am UTC](https://discourse.processing.org/t/controlp5-how-to-set-the-maximum-number-of-characters-in-a-text-field/32062/6 "2021-09-06T08:06:40Z")

</div>

Hi @jhsa,

I guess you can do something like this:

```auto
import controlP5.*;

ControlP5 cp5;

String textValue = "";

void setup() {
  size(700,400);
  
  PFont font = createFont("arial",20);
  
  cp5 = new ControlP5(this);
  
  cp5.addTextfield("input")
     .setPosition(20,100)
     .setSize(200,40)
     .setFont(font)
     .setFocus(true)
     .setColor(color(255,0,0))
     ;
  textFont(font);
}

void draw() {
  background(0);
  fill(255);
  text(cp5.get(Textfield.class,"input").getText(), 360,130);
  text(textValue, 360,180);
  
  text("char counter = " + count, 370,170);
}

int count = 0;
void keyTyped(){
  if(cp5.get(Textfield.class,"input").isActive()){
    if(key == ENTER){
      count = 0;
      cp5.get(Textfield.class,"input").clear(); 
    } else if(key == BACKSPACE || key == DELETE){
      count--;
    } else count ++;
    
    if(count < 0) count = 0;
    else if(count > 10){
      String textString = cp5.get(Textfield.class,"input").getText();
      cp5.get(Textfield.class,"input").setText(textString.substring(0,10));
      count--;
    }
  }
}

```

Basically if you type ENTER, then the counter resets and clears the text field

Hope it helps!

---

<div class="post-metadata">

**Author:** ![jhsa](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@jhsa](https://discourse.processing.org/u/jhsa)\
**Post date:** [September 6, 2021, 1:35pm UTC](https://discourse.processing.org/t/controlp5-how-to-set-the-maximum-number-of-characters-in-a-text-field/32062/7 "2021-09-06T13:35:14Z")

</div>

Hi, thank you for hanging in there. I am sure this topic will help a lot of people because there isn’t much info online about how to do this.  
I have tried your latest suggestion with the “Enter” key, but I am getting the same error after cycling the text field a few times. What would cause this error? Please see photo. Thanks

 ![Editor_Error](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/1/1967f6357b45e4fa8a910c9a1d3792e881a899d1.png)

---

<div class="post-metadata">

**Author:** ![MiguelSanches](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/miguelsanches/32/11172_2.png) [@MiguelSanches](https://discourse.processing.org/u/MiguelSanches)\
**Post date:** [September 6, 2021, 2:05pm UTC](https://discourse.processing.org/t/controlp5-how-to-set-the-maximum-number-of-characters-in-a-text-field/32062/8 "2021-09-06T14:05:21Z")

</div>

Hi @jhsa,

I tested the code that I have sent you and didn’t get the error thrown. If you can’t post the full code I can’t test to find the error. Can you reproduce it in a simpler code?  
Maybe just try having the textfield and see if it still happens?  
The logic for the error is simple. If it is entering the in the substring that throws the error is because the counter is becoming higher than 10 despite the string not having 10 characters. So, something is off.

Best regards!

---

<div class="post-metadata">

**Author:** ![jhsa](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@jhsa](https://discourse.processing.org/u/jhsa)\
**Post date:** [September 6, 2021, 2:29pm UTC](https://discourse.processing.org/t/controlp5-how-to-set-the-maximum-number-of-characters-in-a-text-field/32062/9 "2021-09-06T14:29:39Z")

</div>

It also happens with your code as you can see in the picture. The error appears to be trigger more often when I use the key combination “SHIFT” + " \* ". Not very easy to make it happen.  
Some extra info. I am on Windows 7.  
I am using Processing 4.  
I could post my code, but no way to post a PDE file here, which is not good considering that this is a “Processing” forum. Perhaps the Administrators could allow “.pde” extensions as well? It would make life easier for everybody. Just my 2 cents 🙂  
And thank you so much.

 ![Editor_Error_1](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/0/020bf4d17548fc1dfaf29f754ed2f0cc80c0257c.png)

---

<div class="post-metadata">

**Author:** ![MiguelSanches](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/miguelsanches/32/11172_2.png) [@MiguelSanches](https://discourse.processing.org/u/MiguelSanches)\
**Post date:** [September 6, 2021, 4:08pm UTC](https://discourse.processing.org/t/controlp5-how-to-set-the-maximum-number-of-characters-in-a-text-field/32062/10 "2021-09-06T16:08:17Z")

</div>

I will take a look and let you know tomorrow perhaps!

---

<div class="post-metadata">

**Author:** ![jhsa](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@jhsa](https://discourse.processing.org/u/jhsa)\
**Post date:** [September 6, 2021, 4:09pm UTC](https://discourse.processing.org/t/controlp5-how-to-set-the-maximum-number-of-characters-in-a-text-field/32062/11 "2021-09-06T16:09:49Z")

</div>

Thank You so much Miguel…

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [September 6, 2021, 4:18pm UTC](https://discourse.processing.org/t/controlp5-how-to-set-the-maximum-number-of-characters-in-a-text-field/32062/12 "2021-09-06T16:18:45Z")

</div>

@jhsa

> but no way to post a PDE file here

This is a .pde file; not sure I understand the posting problem?

```auto
color BLUE = color(64,124,188);
    
void setup() {
  fullScreen();
  orientation(LANDSCAPE);
  background(BLUE);
}

void draw() {
  
}

```

---

<div class="post-metadata">

**Author:** ![jhsa](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@jhsa](https://discourse.processing.org/u/jhsa)\
**Post date:** [September 6, 2021, 4:24pm UTC](https://discourse.processing.org/t/controlp5-how-to-set-the-maximum-number-of-characters-in-a-text-field/32062/13 "2021-09-06T16:24:44Z")

</div>

As far as I understand, that is some text you inserted in the text box, not a file that you uploaded.  
And the problem is that there is a character limit on every post.  
My full code has already more than 5 or 6 thousand lines all together. How many posts would I have to make to post the full code then? 😉

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [September 6, 2021, 4:54pm UTC](https://discourse.processing.org/t/controlp5-how-to-set-the-maximum-number-of-characters-in-a-text-field/32062/14 "2021-09-06T16:54:57Z")

</div>

It’s just a simple copy/paste into source code block created by three grave accent characters above and below the code. Hit ‘select all’ in your .pde file then copy/paste the selected text in between the grave accent characters; it’s how we format code in this forum. When your code is posted here there will be a little copy icon in the top right corner so that the folks trying to help you can try to reproduce the problem on their systems. Better yet condense the problem into a simple demo which shows the problem and post that; that way we don’t have to sift through all five to six thousand lines of code.

---

<div class="post-metadata">

**Author:** ![jhsa](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@jhsa](https://discourse.processing.org/u/jhsa)\
**Post date:** [September 6, 2021, 4:59pm UTC](https://discourse.processing.org/t/controlp5-how-to-set-the-maximum-number-of-characters-in-a-text-field/32062/15 "2021-09-06T16:59:39Z")

</div>

I tried before and it won’t fit. Too many characters.  
And how do you make a demo if for example all code is related? Not that easy. It would be easy to just upload the whole thing.

And I mentioned the full code because **most of the people** here ask for it… Just like a few posts above.  
So yes, in my humble opinion, we should be able to upload .PDE files.  
It is up to the admins to allow that of course.

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [September 6, 2021, 5:32pm UTC](https://discourse.processing.org/t/controlp5-how-to-set-the-maximum-number-of-characters-in-a-text-field/32062/16 "2021-09-06T17:32:26Z")

</div>

If you just must post all of the code then another option would be to put the project up on DropBox or Github and post a link.

---

<div class="post-metadata">

**Author:** ![MiguelSanches](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/miguelsanches/32/11172_2.png) [@MiguelSanches](https://discourse.processing.org/u/MiguelSanches)\
**Post date:** [September 7, 2021, 8:09am UTC](https://discourse.processing.org/t/controlp5-how-to-set-the-maximum-number-of-characters-in-a-text-field/32062/17 "2021-09-07T08:09:28Z")

</div>

Hi @jhsa,

So I think the problem had to do with keyCoded keys, like TAB, SHIFT and special characters like ~, where combo keys are used.  
Thus, I decided to change the approach as follows:

```auto
int count = 0;
void keyTyped(){
  if(cp5.get(Textfield.class,"input").isActive()){
    String textString = cp5.get(Textfield.class,"input").getText();
    count = textString.length();
    if(count > 10){
      cp5.get(Textfield.class,"input").setText(textString.substring(0,10));
    }
  }
}

```

Basically, every time you type a key, get the string, count its length and just write the substring if the length is higher than the desired value of 10.

Test it and I hope it works!  
Best regards

PS: if there are special characters that you don’t want the user to be able to use I would suggest you to look into regex (regular expressions) 😉

---

<div class="post-metadata">

**Author:** ![jhsa](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@jhsa](https://discourse.processing.org/u/jhsa)\
**Post date:** [September 7, 2021, 1:58pm UTC](https://discourse.processing.org/t/controlp5-how-to-set-the-maximum-number-of-characters-in-a-text-field/32062/18 "2021-09-07T13:58:28Z")

</div>

This approach seems to work. At least, I still didn’t see any error after trying quite hard to make it happen.  
Also for integrating this code on my code, I guess I could simplify it and drop the “count” variable by using “if(textString.length() \> 10) {” instead?

Yes, there are some special characters that the device does not recognize. Would you please point me to some documentation or examples? Thank you.

> [@MiguelSanches](#):
>
> PS: if there are special characters that you don’t want the user to be able to use I would suggest you to look into regex (regular expressions)

Thank you again and all the best…

---

<div class="post-metadata">

**Author:** ![MiguelSanches](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/miguelsanches/32/11172_2.png) [@MiguelSanches](https://discourse.processing.org/u/MiguelSanches)\
**Post date:** [September 7, 2021, 2:14pm UTC](https://discourse.processing.org/t/controlp5-how-to-set-the-maximum-number-of-characters-in-a-text-field/32062/19 "2021-09-07T14:14:22Z")

</div>

> Also for integrating this code on my code, I guess I could simplify it and drop the “count” variable by using “if(textString.length() \> 10) {” instead?

Yes you can do it like that if you want to

> Yes, there are some special characters that the device does not recognize. Would you please point me to some documentation or examples? Thank you.

Here are a few, but you can and should search for more until you are confident

> **[Java Regular Expressions](https://www.w3schools.com/java/java_regex.asp)**
>
> W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

> **[Java String replaceAll() method - javatpoint](https://www.javatpoint.com/java-string-replaceall)**
>
> Java String replaceAll() method with method signature and examples of concat, compare, touppercase, tolowercase, trim, length, equals, split, string replaceall in java etc.

> <https://stackoverflow.com/questions/336210/regular-expression-for-alphanumeric-and-underscores>

All the best !

---

<div class="post-metadata">

**Author:** ![jhsa](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@jhsa](https://discourse.processing.org/u/jhsa)\
**Post date:** [September 7, 2021, 5:34pm UTC](https://discourse.processing.org/t/controlp5-how-to-set-the-maximum-number-of-characters-in-a-text-field/32062/20 "2021-09-07T17:34:56Z")

</div>

Thank you so much for your time. Really appreciated.
