【问题标题】:How to check if the first character is alpha in JQuery?如何检查 JQuery 中的第一个字符是否为 alpha?
【发布时间】:2017-09-09 21:46:41
【问题描述】:

有什么方法可以检查字段中的第一个字符是否是 Jquery 中的 alpha 字符?

【问题讨论】:

标签: javascript jquery


【解决方案1】:

答案是肯定的,当然……

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Alpha</title>
</head>
<body>
  <input type="text" id="alpha">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
  <script>
    var module = {
      regex: /[a-zA-Z]/,
      str: '',
      init: function () {
        module.listeners();
      },
      listeners: function () {
        $('#alpha').on('input', module.alpha);
      },
      alpha: function () {
        module.str = $('#alpha').val();
        if (module.regex.test(module.str.charAt(0))) {
          console.log('OK');
        } else {
          console.log('KO');
        }
      }
    };

    $(document).ready(module.init);
  </script>
</body>
</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-23
    • 2023-03-24
    • 2012-12-19
    • 2021-09-29
    相关资源
    最近更新 更多