代码之家  ›  专栏  ›  技术社区  ›  Mark

SweetAlert2文本输入,用r shining中的javascript验证文本

  •  0
  • Mark  · 技术社区  · 7 年前

    我正在从SweetAlert升级到r Shining中的SweetAlert2,到目前为止,已经成功地得到r Shining服务器对警报消息中的OK/Cancel按钮的定期响应,但现在我坚持使用下一种类型,即文本输入消息,我以前在其中执行以下验证:

    • 空的
    • 特殊字符的使用 在将值发送到R之前。

    Here 您可以找到一个应用程序,其中实现了sweetelert2。

    在新问题中,我试图用包含输入消息的消息替换该应用程序中的javascript:

    myjava <- "shinyjs.swalFromButton = function(params) { 
          var defaultParams = {
        title: null,
        html : null
        };
        params = shinyjs.getParams(params, defaultParams);
        swal({title : params.title, html : params.html,  
        input: 'text',
        showCancelButton : true,
        showConfirmButton : true,
        closeOnConfirm: false,
        confirmButtonColor: '#339FFF',
        allowOutsideClick: false,
        inputValidator: function(value) {
        if(value === '') { return !value && 'You need to write something!'}
        else {
        var val2= true; 
        Shiny.setInputValue('option2', val2, {priority: 'event'}) };
        }
        });
        };"
    

    到目前为止这是可行的,但我不知道如何添加特殊字符使用的其他检查(在文件名中不允许这样做)。 在我的旧代码中,我有一行用于SweetAlert(1)工作:

     var format = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+/;
     if(format.test(inputValue)){   
     swal.showInputError('Special characters are not allowed');
     return false;
     }
    

    但当我构建这个时,它在sweeter2中不起作用:

    myjava <- "shinyjs.swalFromButton = function(params) { swalFromButton = function(params) {       var defaultParams = {
        title: null,
        html : null
        };
        params = shinyjs.getParams(params, defaultParams);
        swal({title : params.title, html : params.html,  
        input: 'text',
        showCancelButton : true,
        showConfirmButton : true,
        closeOnConfirm: false,
        confirmButtonColor: '#339FFF',
        allowOutsideClick: false,
      inputValidator: function(value) {
    if(value === '') { return !value && 'You need to write something!'}
    else {
              var format = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+/;
                             if(format.test(value)){   
                               return !value && 'Special characters are not allowed'}
                           else {
                             var val2= true; 
    Shiny.setInputValue('option2', value, {priority: 'event'})} 
    }
    }
    });
    };
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Stéphane Laurent    7 年前

    正如另一篇文章所承诺的,这里有一个解决方案 shinyjs :

    library(shiny)
    
    js <- "
    Shiny.addCustomMessageHandler('sweet',
      function(message) {
        swal({
          title : message.title, 
          html : message.html, 
          input : 'text', 
          showConfirmButton : true,
          confirmButtonText : 'Confirm',
          confirmButtonColor: '#00cc00',
          showCancelButton : true,
          cancelButtonText : 'Cancel',
          cancelButtonColor : '#339fff',
          allowOutsideClick: true,
          allowEscapeKey: true,
          inputValidator: function(value) {
            if(value === '') { 
              return 'You need to write something!'
            } else {
              var format = /\\`|\\~|\\!|\\@|\\#|\\$|\\%|\\^|\\&|\\*|\\(|\\)|\\+|\\=|\\[|\\{|\\]|\\}|\\||\\\\|\\'|\\<|\\,|\\.|\\>|\\?|\\/|\"|\\;|\\:/g;
              if(format.test(value)){   
                return 'Special characters are not allowed'
              } 
            }
          }
        })
        .then(function(result){
          if(result.dismiss === swal.DismissReason.cancel) {
            swal('failure');
          } else {
          swal('success');
            Shiny.setInputValue('option1', result.value, {priority: 'event'});
          }
        });
      }
    );
    "
    ui <- basicPage(
      tags$head(tags$script(src = "https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/7.29.2/sweetalert2.all.min.js"),
                tags$link(rel="stylesheet", type="text/css", href = "https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/7.29.2/sweetalert2.min.css"),
                tags$script(js)
      ),
      actionButton("messageButton", "Click me")
    
    )
    
    server <- function(input, output, session){
      observeEvent(input$messageButton, {
        session$sendCustomMessage(type = "sweet",
                                  message = list(title = paste('<span style ="color:#339FFF;">An alert with an input text'),
                                                 html = "Enter text"))
      })
    
      observe({print(input$option1)})
    }
    
    shinyApp(ui, server)
    
        2
  •  0
  •   Mark    7 年前

    好的,解决了,找出正确的语法是什么 去除 !value && 做了这个把戏:

    shinyjs.swalFromButton = function(params) {       var defaultParams = {
        title: null,
        html : null
        };
        params = shinyjs.getParams(params, defaultParams);
        swal({title : params.title, html : params.html,  
        input: 'text',
        showCancelButton : true,
        showConfirmButton : true,
        closeOnConfirm: false,
        confirmButtonColor: '#339FFF',
        allowOutsideClick: false,
        inputValidator: function(value) {
        if(value === '') { 
          return 'You need to write something!'} 
        else {
          var format = /[~!@#$%^&*()_+\-=\[\]{};':"\\|,<>\/?]+/;
          if(format.test(value)){   
             return 'Special characters are not allowed'} 
          }
        }
      })
          .then(function(result){
            if(result.dismiss === swal.DismissReason.cancel) {
            } else {
            Shiny.setInputValue('option2', value, {priority: 'event'})} 
            }
            }
            });
            };