javascript 将删除和箭头键添加到正则表达式中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17164239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add delete and arrow key into Regular Expression
提问by Azam Alvi
I am performing date validation and now I am doing that user can only enter numbers
,/
and backspace
so now I want to add 2 more keys into my regular expression. I want to add delete
and arrow keys
so what will change I should do in my Regular Expression .This is my code
我执行日期验证和我现在做的是用户只能输入numbers
,/
并backspace
因此现在我要2个键添加到我的正则表达式。我想添加delete
,arrow keys
所以我应该在正则表达式中做些什么改变。这是我的代码
<input type="text" id="date" name="date" onkeypress="check(event,this);" />
this is me Javascript code
这是我的 Javascript 代码
<script type="text/javascript">
function check(evt, id)
{
var value = id.value;
var theEvent = evt || window.event;
var key = theEvent.keyCode || theEvent.which;
key = String.fromCharCode( key );
var regex = /[0-9|\b|/]/;
if( !regex.test(key))
{
theEvent.returnValue = false;
if(theEvent.preventDefault)
theEvent.preventDefault();
}
}
</script>
Thanks waiting for your help.
感谢等待您的帮助。
回答by M.G.Manikandan
You can skip the input validation if arrow, delete and backspace keys were pressed
如果按下箭头、删除和退格键,您可以跳过输入验证
function check(evt, id)
{
var value = id.value;
var theEvent = evt || window.event;
var key = theEvent.keyCode || theEvent.which;
// Don't validate the input if below arrow, delete and backspace keys were pressed
if(key == 37 || key == 38 || key == 39 || key == 40 || key == 8 || key == 46) { // Left / Up / Right / Down Arrow, Backspace, Delete keys
return;
}
key = String.fromCharCode( key );
var regex = /[0-9|/]/;
if( !regex.test(key))
{
theEvent.returnValue = false;
if(theEvent.preventDefault)
theEvent.preventDefault();
}
}
回答by CME64
you should use on change and force change onkeyup to check the current value.
您应该使用 on change 和 force change onkeyup 来检查当前值。
mistakes you have:
你有的错误:
1- your regex should be the inverse, your current one checks if the value contains any of these but you want your value to not have other value.
1-您的正则表达式应该是相反的,您当前的正则表达式检查该值是否包含其中任何一个,但您希望您的值没有其他值。
2- you should escape the slash (/) character like this \/ so that it won't be assumed as the end of regex and the rest becomes modifiers!
2- 您应该像这样转义斜杠 (/) 字符 \/ 以便它不会被假定为正则表达式的结尾,而其余部分则成为修饰符!
Example:
例子:
document.getElementById('date').onchange = function(){
var regex = /[^\d\/]/g;
if(regex.test(this.value)) {console.log(false); return false;}
else {console.log(true); return true;}
};
document.getElementById('date').onkeyup = function(){
this.onchange();
};
note: make sure you validate the whole date as dd/mm/yyyy or whatever your format is, right before sumission
注意:确保在提交之前将整个日期验证为 dd/mm/yyyy 或任何格式
回答by nickf
Why don't you just check the actual value of the element, rather than the keypresses which create the value?
为什么不检查元素的实际值,而不是创建值的按键?
You can use the oninput
event for that.
您可以oninput
为此使用该事件。