SitePoint Sponsor |
|
User Tag List
Results 1 to 10 of 10
Thread: Whats Wrong with this Function
-
Jun 19, 2007, 04:43 #1
Whats Wrong with this Function
Hai, whats wrong with this function.
Code:function validate_fields(){ if (feedback.txtname.value==""){ alert("Name is not Optional. Please Enter Your Name."); feedback.txtname.focus(); return false; }elseif (feedback.txtemail.value==""){ alert("Please Enter Your Email Address."); feedback.txtemail.focus(); return false; }elseif (feedback.txtcomments.value==""){ alert("Please type your message."); feedback.txtcomments.focus(); return false; }else{ } }
Code:... onSubmit="return validate_fields(feedback)....
but if i put other elaseif parts,
Code:if (feedback.txtname.value==""){ alert("Name is not Optional. Please Enter Your Name."); feedback.txtname.focus(); return false; }
-
Jun 19, 2007, 04:45 #2
- Join Date
- Oct 2004
- Location
- Birtley, UK
- Posts
- 2,439
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
because your function() doesn't contain a parameter to pass across.
change function validate_fields() to function validate_fields(feedback)
-
Jun 19, 2007, 04:55 #3
Thanks gRoberts for pointing out the problem.
I just put (feedback) as you sad.
but still problem is there.
-
Jun 19, 2007, 04:58 #4
- Join Date
- Oct 2004
- Location
- Birtley, UK
- Posts
- 2,439
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
by the looks of it your passing the form across.
Your best off using document.getElementById('fieldname').value
-
Jun 19, 2007, 05:00 #5
-
Jun 19, 2007, 05:10 #6
gRobert and all
The real problem with my above code is , if i put elseif part it will not work.
but only the below part, works very well.
if (feedback.txtname.value==""){
alert("Name is not Optional. Please Enter Your Name.");
feedback.txtname.focus();
return false;
}
any problem with if ... else
-
Jun 19, 2007, 05:17 #7
- Join Date
- Oct 2004
- Location
- Birtley, UK
- Posts
- 2,439
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
ok...
well if the txtname.value is empty, then the validate_fields will end, as you have return false. This means once it hits return false, it skips whats yet to come.
-
Jun 19, 2007, 05:20 #8
Ok gRob. but problem is, nothing happen when there is elseif added. even if the txtname is empty, it wan't alert.
-
Jun 19, 2007, 05:22 #9
if i remove the bleow part it works.
elseif (feedback.txtemail.value==""){
alert("Please Enter Your Email Address.");
feedback.txtemail.focus();
return false;
}elseif (feedback.txtcomments.value==""){
alert("Please type your message.");
feedback.txtcomments.focus();
return false;
}else{
}
-
Jun 19, 2007, 05:51 #10
oh, it was problem with 'elseif' and it should be 'else if' in java script. i come from php and did not notice this issue.
Thanks gRob for the support. the parameter issue you pointed out is also a mistake i made.
thanks.
Bookmarks