How check the st.text_input len

import streamlit as st
text = st.text_input(“Your text:”)
if len(text) == 2:
st.error(“You can’t use more than 10 characters”)
else:
st.error(“-----------------You can’t use more than 10 characters”)
#st.success(text)

I am submitting my code; however, it does not verify the length of the text and fails to produce the correct result. I kindly request your assistance in providing the appropriate code that checks the length of the text input and clears the text input for new entries or barcodes.
regard

It is not clear what you would consider the correct result. Your code works as expected for me.

Dear boss
The code provided functions correctly when a user inputs data and presses enter; it executes a function, clears the textbox, and sets the focus for new user input. However, I intend to utilize a barcode scanner that produces a 16-character length output. My objective is to ensure that the function verifies whether the character length is 16, and if so, executes the function, sends the barcode to it, clears the textbox, and sets the focus for new barcode scanning.

Currently, the code clears and sets focus only after the enter key is pressed. I would like this process to occur automatically by checking the textbox character length or triggering the function immediately after barcode scanning. I appreciate your assistance in providing the correct code for this functionality.

import streamlit as st
if ‘something’ not in st.session_state:
st.session_state.something = ‘’

def submit():
st.session_state.something = st.session_state.widget
st.session_state.widget = ‘’

st.text_input(‘Something’, key=‘widget’, on_change=submit)

st.write(f’Last submission: {st.session_state.something}')

Something like this should work:

def function(barcode):
    if len(barcode) == 16:
        execute_the_function()
        send_barcode_to_it(barcode)
        st.session_state.key = ""

I don’t think setting the focus programmatically is possible.