...

View Full Version : regex with tab in form



zodehala
09-15-2008, 07:07 PM
<script language="JavaScript1.2">
function check(){
var digit=/^[A-Za-z]+$/;
if (document.myform.myinput.value.search(digit)==-1) //if match failed
alert("0 ile 9 arası rakam gir");
}
</script>
<form name="myform">
<input type="text" name="myinput" size=15 onFocus ="check()"><br/>
<input type="text" name="myinput" size=15 onFocus ="check()">
</form>


when i press TAB i want it apply that regex but i can not

A1ien51
09-15-2008, 07:40 PM
You have two inputs name myinput and when you are referencing your function, you are not specifying an index to pick one of them.

myinput[0] would be the first one
myinput[1] would be the second one

Don't you want to use onblur and not onfocus?

Eric

Philip M
09-15-2008, 07:41 PM
Do you mean when tab is pressed anywhere in the document?

You have two fields with the same name - myinput



<script type = "text/javascript">
document.onkeydown = function(ev) {
var key;
ev = ev || event;
key = ev.keyCode;
if (key == 9) {
check();
}
}
</script>


Why should I do anything for posterity? What has posterity ever done for me? Groucho Marx (1890 - 1977)

zodehala
09-15-2008, 10:47 PM
Do you mean when tab is pressed anywhere in the document?


not anywhere just in textfields

Philip M
09-16-2008, 09:06 AM
<form name= "myform">
<input type = "text" id = "myinput1" onkeyup = "chk()">
<input type = "text" id = "myinput2" onkeyup = "chk()">
</form>

<script type = "text/javascript">
function chk(ev) {
var key;
ev = ev || event;
key = ev.keyCode;
if (key == 9) {
check();
}
}

function check(){
var digit=/^[A-Za-z]+$/;
if (document.myform.myinput1.value.search(digit)==-1) //if match failed
alert("0 ile 9 arasi rakam gir");
}

</script>

But why do you not simply use onblur or onkeyup? I am not too sure what you are trying to achieve.


<form name= "myform">
<input type = "text" id = "myinput1" onkeyup = "check(this)">
<input type = "text" id = "myinput2" onkeyup = "check(this)">
</form>

<script type = "text/javascript">
function check(which){
val = which.value
if (/\d/.test(val)) {
alert("0 ile 9 arasi rakam gir");
len = val.length;
which.value = val.substr(0,len-1); // delete last character
}
}
</script>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum