...

View Full Version : submit a form without a submit button?



dottedquad
07-08-2006, 12:02 AM
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.

JUD
07-08-2006, 01:26 AM
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



<!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>

glenngv
07-08-2006, 06:48 AM
The code can be shorten a bit if you only pass this.

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.

dottedquad
07-08-2006, 04:40 PM
Thanks to the both of you guys. Helped me out greatly!

-Thanks

dottedquad
07-08-2006, 06:34 PM
final 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();

}

}


<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.

glenngv
07-10-2006, 06:52 PM
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)"/>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum