Click to See Complete Forum and Search --> : Validation submits anyway


nwbee
05-29-2003, 03:17 AM
Hi,

I'm having some trouble with my validation.
All fields give a nice popup message,
but if the last field is left blank, it does show up the message that the field must be filled in, but then goes ahead and submits the form anyway...


<b>The function is called like this:</b>
<form name="nieuwsbrief" method="post" action="newslettercreate.asp" onSubmit="return validateForm();">


<b>This is my validation code:</b>
<script>
function validateForm() { //v4.0

// verplicht veld:
if (document.forms[0].subject.value == "")
{self.alert("Je moet iets invullen bij Onderwerp Nieuwsbrief!");
document.forms[0].subject.focus();
return false;}
if (document.forms[0].currentdate.value == "")
{self.alert("Je moet iets invullen bij datum!");
document.forms[0].currentdate.focus();
return false;}
if (document.forms[0].kop1.value == "")
{self.alert("Je moet iets invullen bij Kop Nieuwsitem 1!");
document.forms[0].kop1.focus();
return false;}
if (document.forms[0].item1.value == "")
{self.alert("Je moet iets invullen bij Nieuwsitem 1");
document.forms[0].item1.focus();
return false;}

}

</script>

Quasibobo
05-29-2003, 05:28 AM
I'm missing: return (true); just before the last Tudor arch (accolade in het Nederlands)

Oh and: My validation scripts ends with:

return window.confirm("Formulier verzenden?");

instead of the return(true);

(Ik kan m'n hele script wel ff posten als je er niet uit komt....)

Quasibobo

Charles
05-29-2003, 05:35 AM
I think that I need to see a URL for that page.

nwbee
05-29-2003, 05:49 AM
I've tried the return true...
but it didn't help.....

Charles
05-29-2003, 05:53 AM
Of course not, functions return true by default.

Webskater
05-29-2003, 06:40 AM
You can avoid the problem you describe by doing this:

<form name="nieuwsbrief" method="post" action="newslettercreate.asp" onSubmit="validateForm();">


<b>This is my validation code:</b>
<script>
function validateForm()
{ //v4.0
// verplicht veld:
if (document.forms[0].subject.value == "")
{
self.alert("Je moet iets invullen bij Onderwerp Nieuwsbrief!");
document.forms[0].subject.focus();
}
else if (document.forms[0].currentdate.value == "")
{
self.alert("Je moet iets invullen bij datum!");
document.forms[0].currentdate.focus();
}
else if (document.forms[0].kop1.value == "")
{
self.alert("Je moet iets invullen bij Kop Nieuwsitem 1!");
document.forms[0].kop1.focus();
}
else if (document.forms[0].item1.value == "")
{
self.alert("Je moet iets invullen bij Nieuwsitem 1");
document.forms[0].item1.focus();
}
else
{
document.forms[0].submit()
}
}

</script>

nwbee
05-30-2003, 02:12 AM
I've found the problem.
I tried your code and it gave the exact same problem so I started wondering what could be wrong.

Dum mistake, I'm using html-area in my site (code for online editing functions like bold, alignment etc). with the item1 field.

So it checks if the field is empty, and the field is empty, so it gives the alert.
But I think the field really isn't empty because the html-area will allways create code like:

<html>
<head>
<etc>

How do I check if there is realy something typed in the textarea and not only html-coding?

nwbee
05-30-2003, 02:13 AM
Oh, I forgot. I know that this is the problem (I think) because I changed the textarea to a text field and then it did work...

nwbee
05-30-2003, 02:15 AM
if I just remove the html-area code it works also.
But of course, that's not what I want to do...

Webskater
05-30-2003, 04:03 AM
You could try:

if (document.forms[0].item1.innerText == "")

DaiWelsh
05-30-2003, 04:58 AM
I guess it depends what you are really requiring by way of validation. Clearly it is no longer an empty field since you say there will be

<html>
<head>
<etc>

If the 'blank' code is predictable, then you could check against that and only validate if it is not the same. If you are not quite sure what might be in by default, you could write code to record the initial value of the field and compare against that in the validation, or you could try to strip the html tags from the value and see if anything is left. Like I said it depends really on what you count as valid for that field.

First thing I would do is add an alert to give the value of the field whenever you click submit then experiment with leaving it 'blank' and with adding a few things and see what values are reported by the validation code.

HTH,

Dai

Charles
05-30-2003, 09:06 AM
Originally posted by Dave Clark
Care to site a standard on it?Yes, I was incorrect. By default a function returns nothing. And in the boolean context nothing is evaluated as false. But in the context of an 'onsubmit' or "onclick" event handler nothing is taken to be the same as true. Thanks for calling me on that.

nwbee
06-02-2003, 01:34 AM
I've checked with an alert what content is in the textarea field,
but then it just displays [object] inclucing the brackets?

(Sorry, but I can't give you the url, because is behind passwords for content management)

nwbee
06-02-2003, 07:13 AM
Thanks...
I've tried it, but it gives an empty alert.
So I guess there's no code written there.

I've also checked the output source code and there's no code written there from this field either.

But when I take out the html-area, it all works. How can that be?

nwbee
06-03-2003, 01:17 AM
Oh, I'm sorry.
It's code to create html tags in text areas in forms.

Like this:
<script language="JavaScript1.2" defer>
var config = new Object(); // create new config object

config.toolbar = [
//['fontname'],
//['fontsize'],
//['fontstyle'],
//['linebreak'],
['bold','italic','separator'],
//['bold','italic','underline','separator'],
//['strikethrough','subscript','superscript','separator'],
['justifyleft','justifycenter','justifyright','separator'],
['OrderedList','UnOrderedList','Outdent','Indent','separator'],
//['forecolor','backcolor','separator'],
//['custom1','custom2','custom3','separator'],
['HorizontalRule','separator'],
//['HorizontalRule','Createlink','InsertImage','htmlmode','separator'],
//['about','help']
];

editor_generate('PIC_DESC',config);
</script>

nwbee
06-03-2003, 06:58 AM
I'm using internet explorer 6.0

nwbee
06-03-2003, 07:08 AM
Sorry, is behind passwords and stuff

Quasibobo
06-03-2003, 07:59 AM
This is html-area:

http://www.interactivetools.com/products/htmlarea/

nwbee
06-03-2003, 09:04 AM
It's solved.
I've found another validation function, for other validations too, and that seems to work fine on this field.

Thanks very much for all your help.