【问题标题】:How to allow only string in input text field如何在输入文本字段中只允许字符串
【发布时间】:2019-04-12 10:18:58
【问题描述】:

如何使用 rails haml 表单只允许在 text_field 上输入字符串。

.field
  = f.label "agent_name"
  = f.text_field :agent_name, :required => true,:id=>"agent_name_validation"
  $("#agent_name_validation").keypress(function(event) {
    var string = /^[a-z]+$/i;
    if(event.which != string){
      return false;
    }
  });

【问题讨论】:

    标签: javascript ruby-on-rails ruby dom-events haml


    【解决方案1】:

    使用这个脚本

    $("#agent_name_validation").keypress(function(event){
       var inputValue = event.charCode;
       if(!(inputValue >= 65 && inputValue <= 120) && (inputValue != 32 && inputValue != 0)){
           event.preventDefault();
       }
    });
    

    【讨论】:

      【解决方案2】:

      使用以下 Jquery 函数从文本字段中删除数字

      $("#agent_name_validation").keyup(function(e) {
        // Our regex
        // a-z => allow all lowercase alphabets
        // A-Z => allow all uppercase alphabets
        var regex = /^[a-zA-Z]+$/;
        // This is will test the value against the regex
        // Will return True if regex satisfied
        if (regex.test(this.value) !== true)
        //alert if not true
        //alert("Invalid Input");
      
        // You can replace the invalid characters by:
          this.value = this.value.replace(/[^a-zA-Z]+/, '');
      });
      

      【讨论】:

        【解决方案3】:

        尝试添加 jQuery 验证 gem https://github.com/meowsus/jquery-validation-rails 进行大量验证的方法太简单了 所有验证都可以使用,您只需使用 他们现成的方法

        myField: { lettersonly: true }

        它已经完成了.. 祝你好运..

        【讨论】:

          【解决方案4】:

          如果输入的字符不是regular expression [a-z] 之间的字符串,您可以使用RegExp.prototype​.test() 删除:

          请注意,这将只检查小写字符:

          $('#agent_name_validation').on('input', function () {
            if (!/[a-z]$/.test(this.value)) {
              this.value = this.value.slice(0, -1);
            }
          });
          <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
          <input id="agent_name_validation" type="text">

          【讨论】:

            【解决方案5】:

            @苏曼达斯

            试试这个代码,只允许输入文本字段中的字符串:

            <!DOCTYPE html>
            
                <html>
                <head>
                  <meta charset="utf-8">
                  <meta name="viewport" content="width=device-width">
                  <title>Javascript Demo</title>
                </head>
                <body>
            
                  <form action="/data.php" method="get">
                    <input type="text" name="fullName" onkeypress="return (event.charCode > 64 && 
                    event.charCode < 91) || (event.charCode > 96 && event.charCode < 123)" 
                    placeholder="Full Name">
                    <input type="submit">  
                  </form>
            
                </body>
                </html>
            

            希望以上代码对你有用。

            谢谢。

            【讨论】:

              猜你喜欢
              • 2019-02-10
              • 2014-09-14
              • 1970-01-01
              • 2011-01-30
              • 1970-01-01
              • 2022-11-22
              • 1970-01-01
              • 2017-11-13
              • 1970-01-01
              相关资源
              最近更新 更多