Brian Zhang
Brian Zhang

Reputation: 37

Cannot validate email in shiny

I used the following code to validate the email address in shiny app, but it seems that it does not apply.

I have uploaded it to the shiny server and maybe you can try to type anything in the email column.

https://brianzhang1994.shinyapps.io/Anqi/

Whatever you type in, it does not report error.

ui.R:

library(shiny)
shinyUI(fluidPage(
titlePanel("Please Enter Your Info"),
sidebarLayout(
sidebarPanel(
  textInput("firstname","First Name:"),
  textInput("lastname","Last Name:"),
  dateInput("dob","Date of Birth:",format = "mm-dd-yyyy"),
  textInput("numbers","Cell Phone Numers (10 digits):",value = 1111111111),
  textInput("email","Email Address:")
),

mainPanel(
   dataTableOutput("tableforpatient")
)
)
))

server.R:

library(shiny)
isValidEmail <- function(x) {
grepl("\\<[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,}\\>", as.character(x), 
ignore.case=TRUE)
}

shinyServer(function(input, output) {

output$tableforpatient <- renderDataTable({
validate(
  need(input$firstname !="", 
       paste("First Name: Please Input your firstname")),
  need(input$lastname !="", 
       paste("Last Name: Please Input your lastname")),
  need(nchar(as.numeric(input$numbers)) == 10 , 
       paste("Cell Phone Numers: Please Input a 10-digit-number only"),
  need(isValidEmail(input$email),
       paste("Email Address: Please Input a valid E-mail address"))
       )
)
patient <- data.frame(firstname=input$firstname,
                      lastname=input$lastname,
                      dob=input$dob,
                      number=paste0("+1",input$numbers),
                      email=input$email)
})
})

Upvotes: 2

Views: 1311

Answers (1)

Tonio Liebrand
Tonio Liebrand

Reputation: 17689

You forgot to close the bracket of your "need cell phone numbers" need() function:

need(nchar(as.numeric(input$numbers)) == 10 , 
       paste("Cell Phone Numbers: Please Input a 10-digit-number only")),
need(isValidEmail(input$email),
       paste("Email Address: Please Input a valid E-mail address"))

Upvotes: 1

Related Questions