How to check if a textbox has some valid input or not


#1

i have a textbox. i tried to create something similar to a google form, here the problem is if i give some spaces in the text input, i am able to submit. i would want to be able to check if textinput is empty or not and then be able to submit


#2

What have you tried so far? You should try posting a reprex.

If you just want to check if nothing has been entered into the input from your server code you can add a conditional into a reactive or observe function like this:

if (is.null(input$your_input_id)) {
 # what ever you want to do
}

#3

i figured out a few things, few things still remain. i have restricted that if user enters spaces instead of letters or anything, the form doesn’t submit. but, i want something like, the user shouldn’t be able to enter special characters in the name form

#install.packages("shiny")
library(shinyjs)
library(shiny)
labelMandatory <- function(label) {
  tagList(
    label,
    span("* (Required)", class = "mandatory_star")
  )
}


appCSS <- ".mandatory_star { color: red; }"

fieldsMandatory <- c("name", "qual", "lang")
#ValidationfieldsMandatory <- c("name", "qual", "lang")

fieldsAll <- c("name", "qual", "gender", "lang")
responsesDir <- file.path("C:\\Users\\BPO18\\responses")
epochTime <- function() {
  as.integer(Sys.time())
}

shinyApp(
  
  ui = fluidPage(
    
     shinyjs::useShinyjs(),
     shinyjs::inlineCSS(appCSS),
    
    titlePanel("Shiny form"),
    
    div(
      id = "form",
      
      conditionalPanel(
              condition = "input[name] != '   '  && input[qual] != '   ' && input[lang] != '  '" ),
      
      textInput("name", labelMandatory("Name"), ""),
      
      textInput("qual", labelMandatory("Qualifications"), ""),
      
      textInput("age", "Current age"),
      
      radioButtons("gender", "Gender",c("Male", "Female", "Other")),
      
      selectInput("lang", labelMandatory("Programming languages familiar with"),
                  c("",  "C", "C++", "Python", "JAVA", "R")),
       
      
      actionButton("submit", "Submit", class = "btn-primary")
    )
  ),

  server = function(input, output, session) {
   
   # a<-is.alpha(input) 
     observe({
    mandatoryFilled <-
        vapply(fieldsMandatory,
               function(x) {
                 !is.null(input[[x]]) && input[[x]] != ' '  &&input[[x]] == "a"
               },
               logical(1))
      
      mandatoryFilled <- all(mandatoryFilled)
      
      formData <- reactive({
        data <- sapply(fieldsAll, function(x) input[[x]])
        data <- c(data, timestamp = epochTime())
        data <- t(data)
        data
      })
    
      humanTime <- function() format(Sys.time(), "%Y%m%d-%H%M%OS")
      
      saveData <- function(data) {
        fileName <- sprintf("%s_%s.csv",
                            humanTime(),
                            digest::digest(data))
        
        write.csv(x = data, file = file.path(responsesDir, fileName),
                  row.names = FALSE, quote = TRUE)
      }
      
      # action to take when submit button is pressed
      observeEvent(input$submit, {
        saveData(formData())
      })
      
      
      # enable/disable the submit button
      shinyjs::toggleState(id = "submit", condition = mandatoryFilled)
    })
  }
)