Jump to content

[SOLVED] onSubmit and focus() problem


r-it

Recommended Posts

can someone please help, i have this script:

<script language="javascript">
function valid()
{
var input = document.fmRetail.txName.value;

if(input == "")
{
	alert("Please enter a retail product name before continuing");
	fmRetail.txName.focus();
	return false;
}
else
{
	document.fmRetail.txName.focus();
	return true;
}
}
</script>

which validates if a textbox is empty when one of the submit buttons is clicked, but the problem is that it shows the alert box, but submits the form regardless, what could be the cause of this?Here is the syntax for the form onsubmit:

$show = "<form action=\"$_SERVER[php_SELF]\" method=\"post\" name=\"fmRetail\" onSubmit='return valid()' >

 

and the focus() doesnt seem to work for anything, am I doing it wrong or something

Link to comment
Share on other sites

Unless you've defined it elsewhere ... focus() is not a javascript function.  There is the function "onFocus()", companion to "onBlur()", which is for when someone clicks on the text area.  I did a quick google search for focus() and found these:

 

http://javascript.internet.com/forms/form-focus.html

http://lists.evolt.org/archive/Week-of-Mon-20021125/129068.html

 

Which should give you an idea of how to build the function if you haven't already.  As to why the alert box always pops up, I have no idea.  Try this:

 

var textbox = document.fmRetail.txName;

if(textbox.value == '') {

 

I've always had bad luck defining a value in one line ... no idea why.  If that wasn't the issue, then explain it a little different.  Also, in the if statement, it would be document.fmRetail.txName.focus();

Link to comment
Share on other sites

its cool i got it fixed

 

<script language="javascript">
function valid(f)
{
var input = f.txName.value;

if(input == "")
{
	alert("Please enter a retail product name before continuing");
	f.txName.focus();
	return false;
}
else
{
	f.txName.focus();
	return true;
}
}
</script>

and the form's 1st line

<form action=\"$_SERVER[php_SELF]\" method=\"post\" name=\"fmRetail\" onSubmit='return valid(fmRetail)' >

and it works perfectly, thanks neway nloding

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.