admin管理员组文章数量:1415074
I have a html code which will do the input masking for the date entry which is given below
<html>
<input
type="text"
name="date"
placeholder="dd/mm/yyyy"
onkeyup="
var v = this.value;
if (v.match(/^\d{2}$/) !== null) {
this.value = v + '/';
}
else if (v.match(/^\d{2}\/\d{2}$/) !== null) {
this.value = v + '/';
}
"
maxlength="10"
>
</html>
It takes the input in the specified format and this is working fine,, but I wanted to call this as a function and I tried this
<br><script type="text/javascript">
function check(t){
var v = t;
if (v.match(/^\d{2}$/) !== null) {
t = v + '/';
return;
}
else if (v.match(/^\d{2}\/\d{2}$/) !== null) {
t = v + '/';
return;
}
}
</script>
<html>
<input
type="text"
name="date" style="text-transform: uppercase";
placeholder="dd/mm/yyyy"
onkeyup="check(this.value);"
maxlength="8"
>
</html>
but i didn't get the required result. it is taking the value as ,say 11111111 where it should have taken 11/11/1111
What may be wrong here?
I have a html code which will do the input masking for the date entry which is given below
<html>
<input
type="text"
name="date"
placeholder="dd/mm/yyyy"
onkeyup="
var v = this.value;
if (v.match(/^\d{2}$/) !== null) {
this.value = v + '/';
}
else if (v.match(/^\d{2}\/\d{2}$/) !== null) {
this.value = v + '/';
}
"
maxlength="10"
>
</html>
It takes the input in the specified format and this is working fine,, but I wanted to call this as a function and I tried this
<br><script type="text/javascript">
function check(t){
var v = t;
if (v.match(/^\d{2}$/) !== null) {
t = v + '/';
return;
}
else if (v.match(/^\d{2}\/\d{2}$/) !== null) {
t = v + '/';
return;
}
}
</script>
<html>
<input
type="text"
name="date" style="text-transform: uppercase";
placeholder="dd/mm/yyyy"
onkeyup="check(this.value);"
maxlength="8"
>
</html>
but i didn't get the required result. it is taking the value as ,say 11111111 where it should have taken 11/11/1111
What may be wrong here?
- do you get a js error ? – geevee Commented Nov 20, 2013 at 14:48
- no errors, it is taking the value as say 11111111 where it should have taken 11/11/1111 – Messi L Commented Nov 20, 2013 at 14:50
1 Answer
Reset to default 3You have to give like this , If you want to reflect the changes into textbox
function check(t){
var v = t;
if (v.match(/^\d{2}$/) !== null) {
t = v + '/';
document.getElementById("t1").value=t;
return ;
}
else if (v.match(/^\d{2}\/\d{2}$/) !== null) {
t = v + '/';
document.getElementById("t1").value=t;
return ;
}
}
working fiddle
本文标签: htmlcalling a function for onkeyup event in javascript for input maskingStack Overflow
版权声明:本文标题:html - calling a function for onkeyup event in javascript for input masking - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745232117a2648870.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论