1
library(shiny)    

ui <- fluidPage(
  title = "App Title",
  textInput("text","enter here"),
  uiOutput("text1")
)


max = 10

server <- function(session,input, output) 
{
  output$text1 <- reactive({ paste0('only ', max-nchar(input$text), ' characters left' ) })
  observeEvent(input$text,{
    
    if(nchar(input$text) > max ) { 
      updateTextInput(session , "text",value = substr(input$text,1,max))
    }
  })
}

shinyApp(ui=ui,server=server)

what should I do to stop the cursor when it reaches to the max length provided.I don't need any notification or alert box just the cursor stops and move backward to erase the text.

observeEvent(input$text,{
    
    if(nchar(input$text) > max ) { 
      updateTextInput(session , "text",value = substr(input$text,1,max))
    }
  })

what things should I change to work like it.. took a reference from Add character limit to a text box in an R shiny app

Thank You in advance

  • Currently if the text exceeds 10 characters it deletes the additional characters. What modifications do you want to that? – Ronak Shah Jul 20 '21 at 07:23
  • I want the cursor to stop, it should get disabled when it writes the 10th character and does not write the characters beyond it. – Sakina Zaveri Jul 20 '21 at 14:32

1 Answers1

0

You can run a javascript code in the server to do that.

library(shiny)
library(shinyjs)

ui <- fluidPage(
  title = "App Title",
  useShinyjs(), 
  textInput("text","enter here"),
  uiOutput("text1")
)


max = 10

server <- function(session,input, output) 
{
  output$text1 <- reactive({ paste0('only ', max-nchar(input$text), ' characters left' ) })
  runjs(sprintf("$('#text').attr('maxlength', %d)", max))
}

shinyApp(ui=ui,server=server)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213