简体   繁体   English

如何验证输入类型文本字段作为输入类型时间

[英]How to validate input type text feild as input type time

I want to validate the text field as a time input.我想将文本字段验证为时间输入。 in the text field, I want to show hours and minutes like this.在文本字段中,我想像这样显示小时和分钟。 ex: 12:25例如:12:25

I am already trying some codes.我已经在尝试一些代码。

 function changetrigger(inputField){ var isValid = /^\d{1,2}\/\d{1,2}\/\d{4}$/.test(inputField.value); if (isValid) { console.log('Success'); } else { console.log('Unsuccess'); } return isValid; };
 <div class="col-md-2 col-sm-6 col-6"> <div>Starting Time</div> <input type="text" onchange="changetrigger(this);" id="morning" ng-model="main.morning_r" placeholder="00:00"> </div>

When I run this code error occurred in the console.当我运行此代码时,控制台中发生错误。 "changetrigger is not defined". “未定义更改触发器”。

I am new to code.我是编码新手。 can u help me to fix this or another way to do this validation?你能帮我解决这个问题或其他方式来做这个验证吗?

To validate hours/minutes, you can use regex like:要验证小时/分钟,您可以使用正则表达式,例如:

^([01]?[0-9]|2[0-3]):[0-5][0-9]$

//If you're going to allow one digit in the minutes go with:
^([01]?[0-9]|2[0-3]):([0-5][0-9]|[0-9])$

It will be better to attach the event in your code like the sample below.最好在代码中附加事件,如下例所示。

NOTE: Using the input event is the most efficient way to track the user inputs.注意:使用input事件是跟踪用户输入的最有效方式。

 function changetrigger() { var isValid = /^([01]?[0-9]|2[0-3]):[0-5][0-9]$$/.test(this.value); if (isValid) { console.log('Success'); } else { console.log('Unsuccess'); } return isValid; }; document.getElementById('morning').addEventListener('input', changetrigger);
 <div class="col-md-2 col-sm-6 col-6"> <div>Starting Time</div> <input type="text" id="morning" ng-model="main.morning_r" placeholder="00:00"> </div>

ng-model in your HTML suggests that you are using AngularJS then why not you use ng-keyup / ng-change and declare a scope function in controller? ng-model in your HTML suggests that you are using AngularJS then why not you use ng-keyup / ng-change and declare a scope function in controller? It is better to use this all in angular way.最好以 angular 的方式使用这一切。

//Example
$scope.changetrigger = function() {
     // your logic here
}

Another option is using ng-pattern that can be used with regex.另一种选择是使用可与正则表达式一起使用的ng-pattern

JAVASCRIPT way: JAVASCRIPT方式:

Please Note: If you are trying to mix Javascript with AngularJS then do > not forget to put below changetrigger function inside HTML file's head/script tag OR before the input field so that it initialize before DOM becomes ready. Please Note: If you are trying to mix Javascript with AngularJS then do > not forget to put below changetrigger function inside HTML file's head/script tag OR before the input field so that it initialize before DOM becomes ready. Primafacie, it looks like you have declared that function in controller class and in result it is not defined or the function is declared after the element in HTML file Primafacie, it looks like you have declared that function in controller class and in result it is not defined or the function is declared after the element in HTML file

1- I used onkeyup instead of onchange (for demo) 1-我使用onkeyup而不是onchange (用于演示)

2- I do also change your regex to /\d{2}:\d{2}/ 2-我也将您的正则表达式更改为 /\d{2}:\d{2}/

 function changetrigger(inputField){ var isValid = /\d{2}:\d{2}/.test(inputField.value); if (isValid) { console.log('Success'); } else { console.log('Unsuccess'); } return isValid; };
 <div class="col-md-2 col-sm-6 col-6"> <div>Starting Time</div> <input type="text" onkeyup="changetrigger(this);" id="morning" placeholder="00:00"> </div>

Check this (remeber to include jquery with this)检查这个(记住包括 jquery 这个)

Jquery: Jquery:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.js" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"> 
</script>

 $('.inputField').on('change', function(){ var isValid = /^([01]?[0-9]|2[0-3]):[0-5][0-9]$$/.test($('.inputField').val()); if (isValid) { console.log('Success'); } else { console.log('Unsuccess'); } return isValid; })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="col-md-2 col-sm-6 col-6"> <div>Starting Time</div> <input type="text" class="inputField" id="morning" ng-model="main.morning_r" placeholder="00:00"> </div>

This will eliminate the problem of the function "not being defined".这将消除 function “未定义”的问题。

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

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