Closed Thread Icon

Topic awaiting preservation: Form Validation madness (Page 1 of 1) Pages that link to <a href="http://ozoneasylum.com/backlink?for=8236" title="Pages that link to Topic awaiting preservation: Form Validation madness (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Form Validation madness <span class="small">(Page 1 of 1)</span>\

 
mobrul
Bipolar (III) Inmate

From:
Insane since: Aug 2000

posted posted 07-10-2002 03:28

Alright I've this form with lots of checkboxes:

code:
<input type='checkbox' name='adam'>
<input type='checkbox' name='bill'>
<input type='checkbox' name='chad'>
<input type='checkbox' name='dave'>
<input type='checkbox' name='elvis'>
<input type='checkbox' name='fred'>
<input type='checkbox' name='george'>
<input type='checkbox' name='harry'>


There are also some other parts -- text boxes:

code:
<span>Name:</span><input type='text' name='name'>
<span>E-Mail:</span><input type='text' name='email'>


Now, onSubmit of this form, I'd like to do some validation.
Rules:
Name = imperative
Address = imperative
No more than 3 checkboxes can be checked.
(Think a not so secret ballot)

code:
function validator(theForm){
if (theForm.name.value == ""){
alert("Please enter a value for the 'Name' field");
theForm.name.focus();
}


...and so on for all the other text boxes.
This works great.
I would like to check the checkboxes like this:

code:
var myArray = new Array ('adam', 'bill', 'chad',...and so on);
var j = 0;
function validator(theForm){
for(i=0; i<myArray.length; i++){
if(theForm.myArray[i].checked == true){
j++;
}
}
if(j>3){
alert("You checked too many names!");
theForm.reset();
}
//and so on as above to check for name and address



As I said, when I only check for name, address, etc. everything works out OK, but when I add the checkbox check, it skips the whole validation function altogether and justs posts to the database!

Any help??

mobrul

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 07-10-2002 06:45

Usually what you need to do (and you may be doing this and not posting the code) but when you do a validator with a form field you need to return false to the onSubmit event handler.

it should be
onSubmit="return validator(this)";

and then at in the validator function you need to return true if it validates okay and false if it doesn't I'm not sure where reset or focus figures in here but if it's not submitting on the focus command it's more of a bug. the second one is just resetting the form but still submitting it.



.:[ Never resist a perfect moment ]:.

Nevel
Bipolar (III) Inmate

From: Amsterdam
Insane since: Jun 2002

posted posted 07-10-2002 13:08

Wouldn't it just be easier to go through the validations, and if everything appears to be ok call formName.submit() ?

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 07-10-2002 13:33

Maybe so, but returning false to cancel the submission is more elegant. Also, it will work in browsers with Javascript turned off.

Oh, I know what the problem is. It's this line:

if(theForm.myArray[i].checked == true)

What you need to realize is that this is checking for a property called myArray of the theForm object. The browser expects theForm to have an array called myArray with an entry i with a checked property. But there is no myArray property of the theForm object.

What you mean to do here is get the value of myArray[i], and then get *that* property of the theForm object. So you have a string, and you want to treat it as a property. That is done with the [] operators:

object.property == object["property"]

So what you meant to do, I believe, is this:

if(theForm[myArray[i]].checked == true)

That should fix it.

By the way, you should have gotten an error from this. Do you have your browser set to alert you of errors? This would have probably given you an "object expected" error and given you the specific line number to look at - always very important information.

mobrul
Bipolar (III) Inmate

From:
Insane since: Aug 2000

posted posted 07-10-2002 14:47

Ahhhh! Yes.
Thank you slime. I so appreciate it.

Yes, my form tag looks very much like yours does bitdamaged...I also have 'return (false)' at the end of each of my if statements...immediately after the focus() or reset()...but I do thank you for lookin' out for me!

The problem, slime, with the error line thing, my browser always refreshed before I could go look at the error! I couldn't figure out anyway to get it to not refresh.
Like I said, when the for / if statements were in the code, it acted as if the whole validator function didn't exist. It would even let me post completly empty rows to my database...as if the name and email, etc. checks didn't even exist.

I figured it was something with that line, anyway. It is the piece that does not work, while the rest of it works perfectly.

mobrul

« BackwardsOnwards »

Show Forum Drop Down Menu