SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    971
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Keydown Validation issue

    Hai friends,

    i have a function that validates the enter key is pressed or not.

    Code:
    	function checkEnter(e){ 
    	     var characterCode
    		 if(e && e.which){
    		 e = e
    		 characterCode = e.which
    		 }
    		 else{
    		 e = event
    		 characterCode = e.keyCode
    		 }	 
    		 if(characterCode == 13){
    		 //document.forms[0].submit()
    		 return true;
    		 }else{
    		 return false;
    		 }
        }
    Code:
      <input name='Search' type='submit' id='Search' value='Search' onkeydown="checkEnter(event)" />
    now onkeydown event, i want to call another js function named initialize() if only the checkEnter(event) return true.

    so i dont no the syntax to put in the keydown event. pls help

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2007
    Posts
    566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In that case, you will have to create 1 function that call the checkEvent() and fire initialize depending on the return.

    You can bind several functions to an event using theaddEventListener/attachEvent DOM functions, but you cannot make them run in a specific order.

    This is a reference on the addEventListener, if you want to read it:
    https://developer.mozilla.org/En/DOM...dEventListener

  3. #3
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    971
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thank You tripy,
    let me try it.

  4. #4
    SitePoint Member
    Join Date
    Oct 2008
    Location
    NC
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, I've struggled with a similar problem for days. I think calling the additional function from where the 'return' is detected will work. E.g.,


    Code:
    function checkEnter(e){ 
    	     var characterCode
    		 if(e && e.which){
    		 e = e
    		 characterCode = e.which
    		 }
    		 else{
    		 e = event
    		 characterCode = e.keyCode
    		 }	 
    		 if(characterCode == 13){
    		 //document.forms[0].submit()
                     initit();
    		 return true;
    		 }else{
    		 return false;
    		 }
        }
         function initit(){
          alert("Do Something!");
         }
    On your input if you use
    <
    Code:
    input name='Search' type='submit' id='Search' value='Search' onkeydown="return checkEnter(event)" />
    the 'return' is recognized because it returned 'true'. I've put the code I'm tinkering with at http://centerproto.zapto.org/retexample if you want to look at the source. I hasten to add, this all comes from what I've learned on this and other forums. If anyone can improve on the code, it would be appreciated

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Here's an updated way to detect the keystroke..

    Code html4strict:
    <input name="Search" type="submit" id="Search" value="Search" />

    Code javascript:
    function checkEnter(e) { 
        e = e || window.event;
    	var keyCode = e.keyCode;
        if (keyCode === 13) {
            initit();
    		return true;
        }
        return false;
    }
    function initit() {
        alert("Do Something!");
    }
    window.onload = function () {
        document.getElementById('Search').onkeydown = checkEnter;
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  6. #6
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    971
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    hai guys, i actually had some problem implimenting tripy's code.
    coz i though there must be a simple way that this and i have already done a lot.
    thanks falks, hope this cold simply solve the issue :thumb:


Bookmarks

Posting Permissions

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