|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here! |
|
#1
|
|||
|
|||
|
Form error trapping...
I ahve the following code for error trapping form contents before submission:
<script language="javascript"> function checkform (form) { if (form["name"].value == "") { alert("A name must be entered."); form["name"].focus(); return false ; } if (form["shift"].value == "") { alert("A shift must be entered."); form["shift"].focus(); return false ; } if (form["location"].option == "CLICK HERE") { alert("A location must be selected."); form["location"].focus(); return false ; } return true; } </script> I have 2 problems. 1 - Trying to figure out how to make it so that if under location, which is a form list object, if <option>CLICK HERE</option> is not changed, for the form to give an error message. 2 - Trying to figure out how to make it so that if under location, which is a form list object, if <option>RAMP</option> is selected for it to cross-check and make sure the comment input has text entered. Any ideas? |
|
#2
|
|||
|
|||
|
RE: Form error trapping...
for #1 you need to use the selectedIndex property of the select object (there is no option property)
it would look like this if (form["location"].selectedIndex == 0) { alert("A location must be selected."); form["location"].focus(); return false; } The index starts at zero so if "CLICK HERE" is the first item in the dropdwn then its index is '0' for # 2 just use an else if to see if the selectedIndex for ramp has been selected, if it has check the value of the comment input area the whole thing would look like this if (form["location"].selectedIndex == 0) { alert("A location must be selected."); form["location"].focus(); return false; }else if (form["location"].selectedIndex == 2) { if (form.["comments"].value == ""){ alert("Please enter a comment"); form.["comment"].focus(); return false; } } replace 2 with the index that correspondes to the "RAMP" option |
|
#3
|
|||
|
|||
|
RE: Form error trapping...
The error trapping for the list works if nothing has been selected. Unfortunately, option 2 doesn't work well for me. BElow is the code I have that works right now:
<script language="javascript"> function checkform (form) { if (form["name"].selectedIndex == 0) { alert("A name must be selected from the list."); form["name"].focus(); return false; } if (form["shift"].value == "") { alert("A shift must be entered."); form["shift"].focus(); return false ; } if (form["location"].selectedIndex == 0) { alert("A location must be selected from the list."); form["location"].focus(); return false; } if (form["type"].selectedIndex == 0) { alert("A type must be selected from the list."); form["type"].focus(); return false; } return true; } </script> I need to modify it so that if "type" index is 3, 4 or 5 for it to cross check and make sure something is entered in the comments field. If no entry is made, then focus to the comment after giving the message. |
|
#4
|
|||
|
|||
|
RE: Form error trapping...
use the same principal based on the example I posted
if (form["type"].selectedIndex == 0) { alert("A type must be selected from the list."); form["type"].focus(); return false; }else if ((form["type"].selectedIndex > 2) && (form["type"].selectedIndex < 6)){ if (form.["comments"].value == ""){ alert("Please enter a comment"); form.["comment"].focus(); return false; } } I didnt test it,but it should do what you want it to do |
|
#5
|
|||
|
|||
|
RE: Form error trapping...
I have tried using the code you gave me, and changing it, regardles of what I do, it seems that my if conditions get ignored when I add the code. My code before, that works, except for the cross-reference is:
function checkform (form) { if (form["name"].selectedIndex == 0) { alert("A name must be selected from the list."); form["name"].focus(); return false; } if (form["shift"].value == "") { alert("A shift must be entered."); form["shift"].focus(); return false ; } if (form["location"].selectedIndex == 0) { alert("A location must be selected from the list."); form["location"].focus(); return false; } if (form["type"].selectedIndex == 0) { alert("A type must be selected from the list."); form["type"].focus(); return false; } return true; } The code that doesn't work is: function checkform (form) { if (form["name"].selectedIndex == 0) { alert("A name must be selected from the list."); form["name"].focus(); return false; } if (form["shift"].value == "") { alert("A shift must be entered."); form["shift"].focus(); return false ; } if (form["location"].selectedIndex == 0) { alert("A location must be selected from the list."); form["location"].focus(); return false; } if (form["type"].selectedIndex == 0) { alert("A type must be selected from the list."); form["type"].focus(); return false; } else if ((form["type"].selectedIndex > 2) && (form["type"].selectedIndex < 6)){ if (form.["comments"].value == ""){ alert("Please enter a comment"); form.["comment"].focus(); return false; } } return true; } Once I add the code, none of my conditions work and they get bypassed entirely. Any ideas? |
|
#6
|
|||
|
|||
|
RE: Form error trapping...
as I stated I did not test the code and found errors upon debugging.... the if for the comments should look like this
if (form["comments"].value == ""){ alert("Please enter a comment"); form["comment"].focus(); return false; } you will notice in the original I posted, there were periods between form and ["comments] remove them and it should work fine. I tend to reference form variables differently myself, for the comments field I would use form.comments.value instead of form["comments"].value both do the same thing, but my way takes three less key strokes...lol |
|
#7
|
|||
|
|||
|
RE: Form error trapping...
I like your formatting better. And yes, less keystrucks is better.
Unfortunately, this is still not working for me. Now, my previous conditions work, but when I select a value from my type object, it igoners the condition entirely. Here is the code I have now: function checkform (form) { if (form.name.selectedIndex == 0) { alert("A name must be selected from the list."); form.name.focus(); return false; } if (form.shift.value == "") { alert("A shift must be entered."); form.shift.focus(); return false ; } if (form.location.selectedIndex == 0) { alert("A location must be selected from the list."); form.location.focus(); return false; } if (form.type.selectedIndex == 0) { alert("A type must be selected from the list."); form.type.focus(); return false; } else if ((form.type.selectedIndex > 2) && (form.type.selectedIndex < 6)){ if (form.comments.value == ""){ alert("Please enter a comment"); form.comment.focus(); return false; } } return true; } |
|
#8
|
|||
|
|||
|
RE: Form error trapping...
note here you are using two different names
if (form.comments.value == ""){ alert("Please enter a comment"); form.comment.focus(); return false; } comment & comments Make sure the proper element names are being referenced I will assume by the fact it is not working, that the name is "comment" but I could be wrong |
|
#9
|
|||
|
|||
|
RE: Form error trapping...
Cool. You were right. I guess that after looking at something for plenty of hours, you miss simple mistakes. Sorry about that. Guess there is a reason why they say 2 eyes are better than one.
Thanks! |
![]() |
| Viewing: Codewalkers Forums > Other Technologies > Client Side Things > Form error trapping... |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|
|
|