验证输入的时间只适用于如果我附加了一个断点在chrome调试器
Validate Input for time only works if I attached a break point in chrome debugger
我有一些代码,我想验证输入,我想创建我自己的Validator的时间,而不是使用时间类型的输入,因为我也样式化它。
然而,当前的代码不允许解析所需的字符[a - z],除非我在if语句上放置一个断点。
想要将值转换为始终格式化的time (00:00 and 25:93> 23:59)
window.validateTimeInput = function(evt) {
var e = evt || window.event,
key = e.keyCode || e.which,
keyChar = String.fromCharCode(key),
regexChars = /[0-9]|':/,
regexActions = /37|38|39|40|46|27|13|8/; // Left, Up, Right, Down, Delete, Escape, Enter, Backspace
// BREAK POINT HERE
if( ( regexChars.test(keyChar) ? false : !regexActions.test(key.toString()) ) && e.target.value.length >= 5 ){
e.returnValue = false;
if(e.preventDefault()) e.preventDefault();
//return false;
}
};
<input type="text" onkeydown="validateTimeInput();"/>
的例子:http://jsfiddle.net/labithiotis/YSgUk/1/
问题是你的正则表达式:/37|38|39|40|46|27|13|8/
。这太宽大了。
允许字母"r",键码82。因为/8/
允许字符串"82"
。(也允许字母dnpqrstuvwxyDNPQRSTUVWXY.)
将正则表达式更改为:
/^(37|38|39|40|46|27|13|8)$/
试着从if语句中抽出这一行,看看是否有助于缩小范围。你也可以把你的检查分开一点,而不是试图在一行中运行它们。
$foo = regexChars.test(keyChar) ? false : !regexActions.test(key.toString();
if($foo){
...
}
<?php
// $hd = curl_init("http://www.laleo.com/ebooks_android/ebooks_search_json.php");
// curl_setopt($hd,CURLOPT_POSTFIELDS,"query=deporte");
// curl_exec($hd);
// curl_close($hd);
?>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
var regexp1 = /^[0-2]{1}/;
var regexp2 = /^[0-9]{2}/;
var regexp3 = /^[0-9]{2}':{1}/;
var regexp4 = /^[0-9]{2}':{1}[0-5]{1}/;
var regexp5 = /^[0-9]{2}':{1}[0-9]{2}$/;
$(function(){
$(":text").keyup(function(evt){
var curent = $(this);
if(curent.val().length == 1)
{
if(regexp1.test(curent.val()))
{
}
else
{
curent.val("");
}
}
else if(curent.val().length == 2)
{
if(regexp2.test(curent.val()) && curent.val() < 24)
{
}
else
{
curent.val(curent.val().slice(0,1));
}
}
else if(curent.val().length == 3)
{
if(regexp3.test(curent.val()))
{
}
else
{
curent.val(curent.val().slice(0,2));
}
}
else if(curent.val().length == 4)
{
if(regexp4.test(curent.val()))
{
}
else
{
curent.val(curent.val().slice(0,3));
}
}
else if(curent.val().length == 5)
{
if(regexp5.test(curent.val()))
{
}
else
{
curent.val(curent.val().slice(0,4));
}
}
else if(curent.val().length > 5)
{
curent.val(curent.val().slice(0,5));
}
});
});
</script>
</head>
<body>
<input type="text" />
</body>
</html>
相关文章:
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- jQuery:循环一个具有不同超时值的循环
- 当包含另一个asp文件时,是否也包含所有引用的样式和脚本页面
- 从javascript创建一个列表
- 节点导出返回一个空对象
- 使用clickToggle并在单击另一个元素时关闭元素
- 我可以在json对象中添加一个函数吗
- 使用javascript将动态表从一个html页面打印到另一个html页
- 将jsp文件下拉列表中的选定项分配给一个java变量(比如String selection)
- 表追加而不附加最后一个元素
- 我如何找到一个句子中的所有空格并替换忽略它们
- D3在一个调用中绘制不同的SVG形状,没有可见性
- 如何在android中使用phonegap将文件从一个文件夹移动/复制到另一个文件夹
- 使用类从一个标记中双击事件
- Javascript,访问一个主要对象模块模式中的每个对象
- 如果使用 lodash 将属性存在于另一个对象中,则向对象添加属性
- 我的启动项目中的JS文件的另一个“断点当前不会被命中”错误
- 有没有可能设定一个“手表”?这样,当HTML元素发生变化时,它就会碰到javascript调试器断点
- 添加一个断点在一行中间与Chrome Web Inspector
- 验证输入的时间只适用于如果我附加了一个断点在chrome调试器