【问题标题】:how to check if string contain numbers?如何检查字符串是否包含数字?
【发布时间】:2014-07-19 23:34:10
【问题描述】:

我正在使用 Jquery 验证方法,我想检查字符串(名字或姓氏)是否包含数字
我的代码:

<html>
<head>

  <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.no-icons.min.css" rel="stylesheet">
  <script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>

  <link href="runnable.css" rel="stylesheet" />
  <!-- Load jQuery and the validate plugin -->
  <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  <script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>

  <!-- jQuery Form Validation code -->
  <script>

  // When the browser is ready...
  $(function() {

    // Setup form validation on the #register-form element
    $("#register-form").validate({

        // Specify the validation rules
        rules: {
            firstname: "required",
            lastname: "required",
            email: {
                required: true,
                email: true
            },
            password: {
                required: true,
                minlength: 5
            },
            agree: "required"
        },

        // Specify the validation error messages
        messages: {
            firstname: "Please enter your first name",
            lastname: "Please enter your last name",
            password: {
                required: "Please provide a password",
                minlength: "Your password must be at least 5 characters long"
            },
            email: "Please enter a valid email address",
            agree: "Please accept our policy"
        },

        submitHandler: function(form) {
            form.submit();
        }
    });

  });

  </script>
</head>
<body>
  <h1>Register here</h1>

  <!--  The form that will be parsed by jQuery before submit  -->
  <form action="" method="post" id="register-form" novalidate="novalidate">

    <div class="label">First Name</div><input type="text" id="firstname" name="firstname" /><br />
    <div class="label">Last Name</div><input type="text" id="lastname" name="lastname" /><br />
    <div class="label">Email</div><input type="text" id="email" name="email" /><br />
    <div class="label">Password</div><input type="password" id="password" name="password" /><br />
    <div style="margin-left:140px;"><input type="submit" name="submit" value="Submit" /></div>

  </form>

</body>
</html>

怎么做??
我想如果有人用数字填写他/她的名字或姓氏,或者它包含诸如(1234 或 john1234 或 1234john 或 jo1234hn ..ect)之类的数字,请获取名称不正确的按摩

【问题讨论】:

    标签: jquery validation jquery-validate


    【解决方案1】:

    您可以使用以下 sn-p 检测字符串是否包含数字:

    if(str.matches(".*\\d.*")){
       // The string has a number, do whatever logic you need here.
    } 
    else{
       // The string does not have numbers in it.
    }
    

    【讨论】:

      【解决方案2】:

      Kenneth 说 str.matches 但应该是 str.match

      if(str.match(".*\\d.*")){
         // The string has a number, do whatever logic you need here.
      } 
      else{
         // The string does not have numbers in it.
      }
      

      【讨论】:

        【解决方案3】:

        您可以在没有正则表达式的情况下执行此操作,并向 jQuery 验证器添加自定义方法,例如

        $.validator.addMethod("textOnly",
            function (value, element) {
        
                var numArray = 
                    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
                var containsNumber = false;
        
                $.each(value.split(''), function () {
                    if (numArray.indexOf($(this)[0]) > -1) {
                        containsNumber = true;
                        return false;
                    }
                });
        
                return !containsNumber;
            }, 
           );
        

        fiddle

        【讨论】:

        • @M0136466,没问题,很抱歉小提琴坏了;)
        猜你喜欢
        • 2017-09-23
        • 2013-11-20
        • 1970-01-01
        • 1970-01-01
        • 2020-12-12
        • 2017-02-27
        • 1970-01-01
        • 2015-02-07
        相关资源
        最近更新 更多