View Full Version : Validating on two fields
vwilsonjr
11-08-2002, 01:17 AM
I'm trying to do a very simple thing. All I want to do is to check to see if a form value is set then check to see the condition on two other fields then do an action.
example
Check to see if
form.type1.value !=""
AND
form.fax1.value == ""
OR
form.telephone1.value ==""
IF SO
Display message you must provide a phone number.
HERE IS MY CODE
if (document.form.type1.value == "" && document.form.phone1.value != "" || document.form.fax1.value !="")
{
missinginfo += "\n - Must Provide Division for Telephone or Fax 1";
}
if (document.form.type1.value != "" && document.form.phone1.value == "" || document.form.fax1.value == "")
{
missinginfo += "\n - Must Provide Division for Telephone or Fax Number1";
}
Thanks for you help:mad:
whammy
11-08-2002, 02:49 AM
Your logic is very confusing to me. :) Amendment - your logic isn't that confusing, but your explanation was.
... ok, after reading your logic a few times, I think I know what you're looking for, but I wouldn't use form since that's a reserved word - either use another name for your form or use forms[0] assuming it's the first form on your page...:
if (document.forms[0].type1.value == "" && (document.forms[0].phone1.value != "" || document.forms[0].fax1.value != ""))
{
missinginfo += "\n - Must Provide Division for Telephone or Fax 1";
}
The above says (in pseudo code):
IF "type1" IS empty AND (phone1 has a value OR fax1 has a value) THEN display an error
After rereading your post a few times, that's what I get from it anyway - you want to make sure they put something in "type1" if they fill out either the phone or fax field, right?.
If that's the case, you should explain it like this:
"If someone fills out phone or fax field, but doesn't fill in the "type1" field, I want to display an error." :)
If that doesn't work, can you explain in more detail EXACTLY which fields you want required if certain others are not filled out? But if I guessed right, you're just missing a couple of parentheses.
:D
beetle
11-08-2002, 03:09 AM
Whenever mixing AND and OR, it is wise to group conditionals.
(document.form.type1.value != "" && (document.form.phone1.value == "" || document.form.fax1.value == ""))
Also, if reference is going to be repeated that much, store that reference in a variable and use the variable...
var f = document.form;
(f.type1.value != "" && (f.phone1.value == "" || f.fax1.value == ""))
whammy
11-08-2002, 03:10 AM
Didn't I just display the same thing ?!?
That's why I bolded the parentheses and also used parentheses in my pseudo code to reinforce the idea. Thanks for reinforcing it further though...
But I am fairly sure that form was a reserved word in javascript. I don't have any references handy at the moment to confirm that though...
:confused:
Edit: I see it is not a reserved word (exactly), but now I remember why it caused me grief:
http://www.javascripter.net/faq/reserved.htm
http://www.forerunners.org/WebLibrary/jscript/ch02_07.htm
vwilsonjr
11-08-2002, 12:30 PM
Thanks for the input. This is the first time I've used javascripts to do error checking. Your input was helpful, and it made me come to the conclusion that my logic was off. I needed && and not ||. Thanks anyway for the other input.
:thumbsup:
beetle
11-08-2002, 01:37 PM
Originally posted by whammy
Didn't I just display the same thing ?!?Yes, I had hit 'Reply', typed my message, and had to step away from my computer, then came back some time later and hit 'Submit Reply'. During that time you obviously posted :D
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.