无法验证闪亮的电子邮件

Cannot validate email in shiny

我在闪亮的应用程序中使用了以下代码来验证电子邮件地址,但它似乎不适用。

我已经把它上传到闪亮的服务器上了,也许你可以尝试在电子邮件栏中输入任何内容。

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

无论你输入什么,它都不会报错。

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)
})
})

您忘记关闭 "need cell phone numbers" need() 函数的括号:

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"))