/    Sign up×
Community /Pin to ProfileBookmark

need help with JS validation

HI Everyone,

I’m hoping someone out there will be able to tell me what’s wrong here. I’ve got a web-based db, and I’m having a problem with the input validation. When I check the form, everything works fine. Here’s the scenario:

There are 2 fields (radio buttons) on the form: RM_OR_Death and RM_OR_Injury. If the user selects “No” for RM_OR_Death and “Yes” for RM_OR_Injury, then it’s required for the user to select something from the RM_OR_Nature_Of_Injury field (dialog list). The first value in the list is “None|” (that’s None with a pipe character after it, no alias).

The problem is that even though the input validation works EVERY TIME when I test it, somehow users (and not always the same user) are able to submit a document even though they’ve selected “No” for RM_OR_Death, “Yes” for RM_OR_Injury, and “None” for RM_OR_Nature_Of_Injury. Again, every time I try this, the input validation works perfectly. I’m pasting the code being used below, Hopefully, someone will be able to spot an error. I’ve been working on this for 3 days now with the other developers (most of whom have far more web & JS experience than I) and we haven’t been able to come up with anything.

Thanks in advance (and sorry for the line wraps),

Steve

(note – the body part section below isn’t a problem as far as I know, it’s just included to show the end of the function)

// RM_OR_Injury[0]=Yes RM_OR_Injury[1]=No
// Injury field is hidden when Death is selected; verify presence of Iinjury field
if (form.RM_OR_Injury) {

alert(“Nature of Inj TXT= [” + form.RM_OR_Nature_Of_Injury[form.RM_OR_Nature_Of_Injury.selectedIndex].text + “]nNature of Inj Val= [” + form.RM_OR_Nature_Of_Injury[form.RM_OR_Nature_Of_Injury.selectedIndex].value + “]”)
// check for default as no selection
// if (form.RM_OR_Nature_Of_Injury[form.RM_OR_Nature_Of_Injury.selectedIndex].text == form.RM_OR_Nature_Of_Injury[0].text ) {
if (form.RM_OR_Nature_Of_Injury.selectedIndex==-1 || form.RM_OR_Nature_Of_Injury[form.RM_OR_Nature_Of_Injury.selectedIndex].text == ‘None’ || form.RM_OR_Nature_Of_Injury[form.RM_OR_Nature_Of_Injury.selectedIndex].value== ” ) {
if (form.RM_OR_Injury[0].checked) {
allErrMsg = allErrMsg + ” * Select best description for nature of injury n”;
if (nSub==””) { nSub=”RM_OR_Nature_Of_Injury” }
} else {
// ok so far
}
} else {
// some injury-type is selected
if (form.RM_OR_Injury[0].checked) {
// no problem
} else {
// automatically assure Injury=YES whenever a known injury-type is seleced
if (form.RM_OR_Nature_Of_Injury[0].text == “Unknown”) {
// never mind
} else {
alert(“NOTE – Bodily Injury changed to YESnn based on Nature of Injury”);
form.RM_OR_Injury[0].checked= true;
}
}
}
// Similar verification for Body Part injured when Injury field is present; introduced Jan 2005
if (form.RM_OR_BodyPart[form.RM_OR_BodyPart.selectedIndex].text == form.RM_OR_BodyPart[0].text ) {
// AR# 457047 – “The body-part field needs to be changed so it is not a required field.”
// if (form.RM_OR_Injury[0].checked) {
// allErrMsg = allErrMsg + ” *
Select closest body part for location of injury n”;
// if (nSub==””) { nSub=”RM_OR_BodyPart” }
// } else {
// // ok so far
// }
} else {
// some body part is selected
if (form.RM_OR_Injury[0].checked) {
// no problem
} else {
// automatically assure Injury=YES whenever an body pary is seleced
alert(“NOTE – Bodily Injury changed to YESnn based on Body Part injured”);
form.RM_OR_Injury[0].checked= true;
}
}
} // end IF RM_OR_Injury exists

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@Arty_EffemAug 28.2006 — (and sorry for the line wraps),[/quote]Just use [B]code[/B] tags.
[CODE]if (form.RM_OR_Injury)[/CODE][/QUOTE]To me this reads like a test for the [I]presence[/I] of the set of radio buttons called RM_OR_Injury, that you say you have in the form.

Under what circumstances would you expect this test to fail?
Copy linkTweet thisAlerts:
@stevemtnoauthorAug 29.2006 — Pardon my newbie-ness but, code tags?

As far as when it should fail, if the user selects "No" for RM_OR_Death, "Yes" for RM_OR_Injury, and "None" for RM_OR_Nature_Of_Injury, then the input validation should kick in and keep them from submitting the form.

*Every time* I test it, it works for me. Still, documents are being submitted that meet all three field values above...

Thanks,

Steve
Copy linkTweet thisAlerts:
@Arty_EffemAug 29.2006 — Pardon my newbie-ness but, code tags?[/quote][URL]http://www.webdeveloper.com/forum/misc.php?do=bbcode[/URL]

[CODE]if (form.RM_OR_Injury)[/CODE]Should I understand that the existence of this element is dependent upon one or more conditions?

Either way it's clear that you haven't shown enough of this abysmal code to form a proper conclusion. At minimum one would need to see the HTML for entire form and all the script(s) associated with it.

This should have occurred to me earlier, but is there any chance that those managing to submit under the wrong conditions, might be among the 5-10% with JS disabled?
Copy linkTweet thisAlerts:
@stevemtnoauthorAug 29.2006 — 

Should I understand that the existence of this element is dependent upon one or more conditions?[/QUOTE]


Only the conditions I mentioned earlier.

This should have occurred to me earlier, but is there any chance that those managing to submit under the wrong conditions, might be among the 5-10% with JS disabled?[/QUOTE]

No, I tried that. If JS is disabled the form cannot be submitted. The only thing I've been able to determine that is the same from document to document is that they're all being submitted via IE6.

Either way it's clear that you haven't shown enough of this abysmal code to form a proper conclusion. At minimum one would need to see the HTML for entire form and all the script(s) associated with it.[/QUOTE]

Well, I can't take any credit for the 'abysmal code'. This is just a problem I was given to work on. I'm trying to figure it out too.

The HTML and JS is too large to paste in here, so I attached a zip file containing both. The HTML is first - the JS will be below that.

Thanks in advance for your help. If you need any additional information, let me know.

stevemtno

[upl-file uuid=beab2585-7e95-4ada-b20f-bde52a9ace08 size=36kB]html_js.zip[/upl-file]
×

Success!

Help @stevemtno spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.23,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...