Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

stop form from submitting

Status
Not open for further replies.

playerx

Technical User
Joined
Feb 23, 2006
Messages
10
Location
US
I have this script that alerts when a textfield is empty

problem is the form still submits. I want to prevent it from submiting

Is there a way to stop the form from submitting?
when the textfield is empty


<script Language="JavaScript">
<!--
function Blank_TextField_Validator(form1) {
if (form1.contact.value == "")
{
alert("Please fill in the Contact field.");
//form1.contact.focus();
form1.submit();
return false;
}
return false;
}
//return (true);

//-->
</script>


<form name="form1" method="POST" action="<%=MM_editAction%>" onsubmit="return Blank_TextField_Validator(this)">
 
don't have the :

form1.submit();

line in the if field is blank statement.
 
with proper indentation we see a few things:
Code:
<script Language="JavaScript">
<!--
function Blank_TextField_Validator(form1) {
   if (form1.contact.value == "") {
      alert("Please fill in the Contact field.");
      //form1.contact.focus();
      form1.submit(); [!][small]<-- calling submit even though the form was not valid (remove this)[/small][/!]
      return false;
   }
   return false;[!][small]<-- return true should be here if it passed validation[/small][/!]
}
//return (true); [!][small]<-- return true outside the function[/small][/!]

//-->
</script>  


<form name="form1" method="POST" action="<%=MM_editAction%>" onsubmit="return Blank_TextField_Validator(this)">

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
Ok I made changes, but the form still does not stop.
It still submits.
Is there something I'm missing?


<script Language="JavaScript">
<!--
function Blank_TextField_Validator(form1) {
if (form1.contact.value == "")
{
alert("Please fill in the Contact field.");
//form1.contact.focus();
return false;
}
return (true);
}

//-->
</script>
 
ensure that the field name really is "contact" and that is really does exist between your [tt]<form></form>[/tt] tags.

are you getting any javascript errors? maybe an undefined error? do you have a link?



*cLFlaVA
----------------------------
spinning-dollar-sign.gif
headbang.gif
spinning-dollar-sign.gif

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
playerx said:
Ok I made changes, but the form still does not stop.
It still submits.
Is there something I'm missing?

Quite possibly.

Apart from cLFlaVA's excellent suggestion in his last post (checking form element name and position - something you've never shown us), check these things:

- are you sure the field is blank? Maybe it has some whitespace in it

- do you actually have JavaScript enabled? If not, of course this will never run.

Hope this helps,
Dan




[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Any luck with this one, playerx? If you found the posts useful, let those that attempted to help you (using their own time and resources for free) know your appreciation.

It would also be nice if you could summarize the solution that worked for you and post it as a followup (for the next person researching the same problem).

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
For Playerx, and anyone else who's having a similar problem. Here is an open-source JavaScript from webmonkey.com's JavaScript Library resource:

Code:
<script language="JavaScript">
<!--

[COLOR=red]\/** everything in between here...**\/[/color]
// -----------------------------------------------------------------
// Function    : IsFormComplete
// Language    : JavaScript
// Description : Checks if all elements in a form have a non-blank value
// Copyright   : (c) 1998 Shawn Dorman
// [URL unfurl="true"]http://www.goodnet.com/~sdorman/web/IsFormComplete.html[/URL]
// -----------------------------------------------------------------
// Ver    Date    Description of modification
// --- ---------- --------------------------------------------------
// 1.0 08/31/1996 Original write
// 1.1 09/30/1998 CHG: Use standard header format
// -----------------------------------------------------------------
// Source: Webmonkey Code Library
// ([URL unfurl="true"]http://www.hotwired.com/webmonkey/javascript/code_library/)[/URL]
// -----------------------------------------------------------------
[COLOR=red]^--...and here is webmonkey's copyright info, it doesn't affect script function(commented out)--^

function IsFormComplete(FormName)
{
var x       = 0
var FormOk  = true

while ((x < document.forms[FormName].elements.length) && (FormOk))
   {
     if (document.forms[FormName].elements[x].value == '')
     { 
        alert('Please enter the '+document.forms[FormName].elements[x].name +' and try again.')
        document.forms[FormName].elements[x].focus()
        FormOk = false 
     }
     x ++
   }
return FormOk
}



// -->

</script>
If I understand your original question right, this scrit will do everything you want done for your form, and it will work on a form with any number of inputs(not just, necessarily, one)! :-)


SIDE NOTE: I noticed in your script
Code:
<script Language="JavaScript">
<!--
function Blank_TextField_Validator(form1) {
if (form1.contact.value == "")
{
alert("Please fill in the Contact field.");
//form1.contact.focus();
return false;
}
return (true);
}

//-->
</script>
that you commented out the [tt]form1.contact.focus();[/tt] line, this will stop the script from moving the system cursor to the unfilled textbox in the end...just a note in case you hadn't intentionally disabled that line.


Hope This Helped;
Rob Hercules
 
Boy, playerx really does like to abandon his/her threads, huh? I think I'm done answering his/her questions. Thanks for digging those up jeff [thumbsup2]

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top