简体   繁体   English

javascript检查特定字符

[英]javascript check for specific characters

I am trying to create a timer in javascript, and I have got it working, however I am trying to validate the users input, for the time. 我正在尝试使用javascript创建一个计时器,并且该计时器已正常运行,但是我正在尝试验证用户输入的时间。 Currently it will accept anything as long as the inputbox is not empty. 当前,只要输入框不为空,它将接受任何内容。 But I am wanting to only allow numbers, colon (:) and periods (.), I have looked at several questions, but most seem to be only checking for all text characters. 但是我只想允许数字,冒号(:)和句点(。),我已经看了几个问题,但大多数似乎只检查所有文本字符。

Here is the working code: https://jsfiddle.net/hLqayL1w/ 这是工作代码: https : //jsfiddle.net/hLqayL1w/

OR 要么

HTML: HTML:

<div class="maincont">
  <h2>Please enter a amount of time</h2>
  <p id="timer">0:00</p>
  <div class="container">
    <input id="request" type="text" placeholder="Time"> &nbsp; &nbsp;
    <button type="submit" class="click">Start timer</button>
  </div>
  <P id="cancelbutton" class="cancel">Cancel timer</P>
</div>

JS: JS:

$(document).ready(function() {
  $('#cancelbutton').hide();
});

$('.click').click(function() {
  var conts = $('#request').val();
  if ($('#request').val() === "") {
    return;
  }
  $('.container').hide();
  $('#cancelbutton').fadeIn('slow');
  var rawAmount = $('#request').val();
  var cleanAmount = rawAmount.split(':');
  var totalAmount = parseInt(cleanAmount[0] | 0) * 60 + parseInt(cleanAmount[1] | 0);
  $('#request').val(" ");

  var loop, theFunction = function() {

    totalAmount--;

    if (totalAmount == 0) {
      clearInterval(loop);
      $('#cancelbutton').hide();
      $('.container').fadeIn('slow');
    }
    var minutes = parseInt(totalAmount / 60);
    var seconds = parseInt(totalAmount % 60);

    if (seconds < 10)
      seconds = "0" + seconds;
    $('#timer').text(minutes + ":" + seconds);

    $('#cancelbutton').click(function() {
      totalAmount = 1
    });
  };

  var loop = setInterval(theFunction, 1000);
})

In your example you just check if the input is an empty string lets change that so we check if it matches an regular expression: 在您的示例中,您只需检查输入是否为空字符串就可以进行更改,因此我们检查它是否与正则表达式匹配:

if (!$('#request').val().match(/[0-9:.]/gi)) {
    return;    
}

We make it look for numbers,colons and dots everything else will not match and the return will run. 我们让它寻找数字,冒号和点,其他所有不匹配的内容都会返回。

Here is an working example. 是一个工作示例。

(This is just an quick and dirty example it is not showcasing that the regex should be written in this way, there probably are better ways to write this) (这只是一个快速而肮脏的示例,它没有显示正则表达式应该以这种方式编写,可能有更好的方式编写此代码)

How about this? 这个怎么样?

     $('#request').keyup(function() { 
    var el = $(this),
        val = el.val();

     el.val(val.replace(/[^\d\:.]/gi, ""));
 }).blur(function() {
     $(this).keyup();
 });

It will check check if the input is number/./:, if not it will remove it live. 它将检查输入是否为数字/./ :,否则将实时删除它。

    $(document).ready(function() {
  $('#cancelbutton').hide();
});
     $('#request').keyup(function() { 
        var el = $(this),
            val = el.val();

         el.val(val.replace(/[^\d\:.]/gi, ""));
     }).blur(function() {
         $(this).keyup();
     });
$('.click').click(function() {
  var conts = $('#request').val();
  if ($('#request').val() === "") {
    return;
  }
  $('.container').hide();
  $('#cancelbutton').fadeIn('slow');
  var rawAmount = $('#request').val();
  var cleanAmount = rawAmount.split(':');
  var totalAmount = parseInt(cleanAmount[0] | 0) * 60 + parseInt(cleanAmount[1] | 0);
  $('#request').val(" ");

  var loop, theFunction = function() {

    totalAmount--;

    if (totalAmount == 0) {
      clearInterval(loop);
      $('#cancelbutton').hide();
      $('.container').fadeIn('slow');
    }
    var minutes = parseInt(totalAmount / 60);
    var seconds = parseInt(totalAmount % 60);

    if (seconds < 10)
      seconds = "0" + seconds;
    $('#timer').text(minutes + ":" + seconds);

    $('#cancelbutton').click(function() {
      totalAmount = 1
    });
  };

  var loop = setInterval(theFunction, 1000);
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM