Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Dec 2007
    Posts
    269
    Thanks
    28
    Thanked 0 Times in 0 Posts

    regex with tab in form

    Code:
    <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

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    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
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,245
    Thanks
    203
    Thanked 2,555 Times in 2,533 Posts
    Do you mean when tab is pressed anywhere in the document?

    You have two fields with the same name - myinput


    Code:
    <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)

  • #4
    Regular Coder
    Join Date
    Dec 2007
    Posts
    269
    Thanks
    28
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Do you mean when tab is pressed anywhere in the document?
    not anywhere just in textfields

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,245
    Thanks
    203
    Thanked 2,555 Times in 2,533 Posts
    Code:
    <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.

    Code:
    <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>
    Last edited by Philip M; 09-16-2008 at 09:18 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •