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 6 of 6
  1. #1
    New Coder
    Join Date
    Nov 2004
    Posts
    95
    Thanks
    0
    Thanked 0 Times in 0 Posts

    submit a form without a submit button?

    Hello, I was wondering how I could submit a form without using a button? What I'm trying to do is have javascript constently check to make sure my textfield has 10 numerical characters in it. If the text field doesn't have numerical characters in it clear it and set focus back to the text field. The set focus is used just incase the user decides to hit the tab key or any other key to unfocus the text field. Once there is 10 numerical characters in the field submit the form automatically.

    This is my code so far:

    <script language="JavaScript">
    function checkid(idvalue) {
    if (idvalue.length != 10)
    {
    var field = document.forms[0].memid;
    field.value = "";

    }
    }
    </script>
    </head>

    <input name="memid" type="text" onchange="checkid(me.value)"/><br />

    For some reason when I type in anything in the text field my checkid function isn't getting executed...

    -Thanks for the help.
    Last edited by dottedquad; 07-08-2006 at 12:38 AM.

  • #2
    JUD
    JUD is offline
    New Coder
    Join Date
    Jul 2005
    Location
    I'm right here
    Posts
    92
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Firstly me.value should be this.value in your onchange event handler.

    Secondly, if you want to fire the function when someone hits the tab key etc. you should use onblur instead of onchange

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    <script type="text/javascript">
    // <![CDATA[ //
    	function checkid(idvalue){
    		if(idvalue.length != 10){
    			document.forms[0].memid.value = "";
    			document.forms[0].memid.focus();	
    		}else{
    			document.forms[0].submit();
    		}
    	}
    // ]]> //
    </script>
    </head>
    
    <body>
    <form>
    <input type="text" name="memid" onblur="checkid(this.value)" />
    </form>
    </body>
    </html>
    Last edited by JUD; 07-08-2006 at 01:33 AM.

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,030
    Thanks
    0
    Thanked 247 Times in 244 Posts
    The code can be shorten a bit if you only pass this.
    Code:
    function checkid(fld){
    	if(fld.value.length != 10 || isNaN(fld.value)){
    		fld.value = "";
    		fld.focus();	
    	}else{
    		fld.form.submit();
    	}
    }
    ...
    <input type="text" name="memid" onblur="checkid(this)" />
    BTW, validating form on onsubmit event is better than field onblur.

  • #4
    New Coder
    Join Date
    Nov 2004
    Posts
    95
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks to the both of you guys. Helped me out greatly!

    -Thanks

  • #5
    New Coder
    Join Date
    Nov 2004
    Posts
    95
    Thanks
    0
    Thanked 0 Times in 0 Posts
    final code:
    Code:
      function checkid(idvalue)
      {
      
        var regstr = /[^\d]/;
        var field = document.forms[0].memid;
        var rgex = field.value.search(regstr);
    	
        if ( rgex > -1 )
        {
    	  
          field.value = "";
          field.focus(); /*used just incase the user hits a key to unfocus the textfield*/
       
        } else if ( idvalue.length == 10 ) 
        {
    	  
          field.form.submit();
    	  
        }
    	  
      }
    Code:
    <input name="memid" type="text" onkeyup="checkid(this.value)"/>
    i tried using checkid(this) but it wasn't passing the value of the memid value so i went back to this.value.

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,030
    Thanks
    0
    Thanked 247 Times in 244 Posts
    Code:
    function checkid(field)
      {
      
        var regstr = /[^\d]/;
        var rgex = field.value.search(regstr);
    	
        if ( rgex > -1 )
        {
    	  
          field.value = "";
          field.focus(); /*used just incase the user hits a key to unfocus the textfield*/
       
        } else if ( field.value.length == 10 ) 
        {
    	  
          field.form.submit();
    	  
        }	  
      }
    ...
    <input name="memid" type="text" onkeyup="checkid(this)"/>


  •  

    Posting Permissions

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