Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IF and statement

Status
Not open for further replies.

fergy1

IS-IT--Management
Jul 25, 2003
32
0
0
US
Hello-

I keep getting an error on the and statement below any ideas? I'm checking one field to see if its been checked yes, if so than the second field auto_count needs filled in.

function DoCustomValidation()
{
var frm = document.forms["myform"];
if(frm.own_auto.value == "Y") && (frm.auto_count.value == "")
{
alert('Please fill out Auto Count!');
return false;
}
else
{
return true;
}
}
 
Missing brackets:
Code:
function DoCustomValidation()
{
  var frm = document.forms["myform"];
  if[red]([/red](frm.own_auto.value == "Y") && (frm.auto_count.value == "")[red])[/red]
  {
    alert('Please fill out Auto Count!');
    return false;
  }
  else
  {
    return true;
  }
}

by the way, posting code in [tt][blue][ignore]
Code:
[/ignore][/blue][/tt] tags makes it easier to read :)

---
Marcus
better questions get better answers - faq581-3339
accessible web design - zioncore.com
 
Thanks for the help and suggestion!
 
How can I make this statement work? I can get auto count or Own_auto by themselves to work but when i check for both conditions it wont work. I dont get errors just nothing happens?

Code:
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function verify() {
var themessage = "You are required to complete the following fields: ";
if (document.form.first.value=="") {
themessage = themessage + " - First Name";
}
if (document.form.last.value=="") {
themessage = themessage + " -  Last Name";
}
if (document.form.email.value=="") {
themessage = themessage + " -  E-mail";
}
if (document.form.auto_count.value == "") && (document.form.own_auto.value == "Y")) {
themessage = themessage + " -  Auto Count";
}
//alert if fields are empty and cancel form submit
if (themessage == "You are required to complete the following fields: ") {
document.form.submit();
}
else {
alert(themessage);
return false;
   }
}
//  End -->
</script>
 
Still missing a bracket:
Code:
if [red]([/red](document.form.auto_count.value == "") && (document.form.own_auto.value == "Y")) {

By the way, I find [blue][tt]+=[/tt][/blue] is a useful notation - instead of:
[tt][blue]themessage = themessage + " - E-mail";[/blue][/tt]
you can use
[tt][blue]themessage += " - E-mail";[/blue][/tt]

---
Marcus
better questions get better answers - faq581-3339
accessible web design - zioncore.com
 
Even with the bracket no go. It ignores it. The own_auto field is a radio button but it should still see it right? Own_Auto is a text field
 
Here's my code, tested and working in IE6 and FF1.5:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd">[/URL]
<html>
<head>

<script type="text/JavaScript">

function verify() {
var themessage = "You are required to complete the following fields: ";
if (document.form.first.value=="") {
themessage += " - First Name";
}
if (document.form.last.value=="") {
themessage +=" -  Last Name";
}
if (document.form.email.value=="") {
themessage +=" -  E-mail";
}
if ((document.form.auto_count.value == "") && (document.form.own_auto.value == "Y")) {
themessage += " -  Auto Count";
}
//alert if fields are empty and cancel form submit
if (themessage == "You are required to complete the following fields: ") {
document.form.submit();
}
else {
alert(themessage);
return false;
   }
}


</script>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>

<form action="#" name="form" onsubmit="verify();">
	first:<input name="first"><br>
	last:<input name="last"><br>
	email:<input name="email"><br>
	own_auto:
	<select name="own_auto">
		<option value="Y" selected>Yes</option>
		<option value="N">No</option>
	</select>
	<br>
	auto_count:<input name="auto_count"><br>
	<input type="submit" value="go">
</form>
</body>
</html>

Disclaimer: this code doesn't take into account best practice HTML such as use of label,etc - it's purely a demonstration and POC for the javascript.

---
Marcus
better questions get better answers - faq581-3339
accessible web design - zioncore.com
 
As long as you have a space after the word "if" the outer set of parentheses should not be necessary, although they certainly don't hurt.

FYI: [tt][!]([/!][/tt] and [tt][!])[/!][/tt] are not brackets, they're parentheses. [tt][!][[/!][/tt] and [tt][!]][/!][/tt] are brackets. [tt][!]{[/!][/tt] and [tt][!]}[/!][/tt] are braces. Just to keep things accurate.


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
As long as you have a space after the word "if" the outer set of parentheses should not be necessary, although they certainly don't hurt.

Woah there cowboy!
The outer set absolutely are necessary:

[tt][blue]if (true) && (true) {[/blue][/tt]
returns a syntax error.

[tt][blue]if ((true) && (true)) {[/blue][/tt]
is permitted, as is

[tt][blue]if (true && true) {[/blue][/tt]
or
[tt][blue]if (a==true && b==true) {[/blue][/tt]



The outer parenthesis are always essential; the inner ones help readability, and clarify operator precedence.


Oh, as for my use of 'bracket', please excuse me - I'm British ;-)
from answers.com:
brack·et (br?k'?t)
n.
5. Chiefly British. One of a pair of parentheses.

---
Marcus
better questions get better answers - faq581-3339
accessible web design - zioncore.com
 
Whoops! You're right, it's the inner set that are optional. I guess I'm confusing my languages again! Sometimes working in three or four languages at the same times can be SO confusing! [bugeyed]

And I see the old English vs. English issue rises again! I always thought one of a pair of parentheses was a parenthesis.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Oh, we also refer to a pair of parentheses as 'brackets'. It's easier to pronounce in Maths class!

In British English, [ ] are square brackets, ( ) are brackets, and < > are angled brackets. Although we also accept that [ ] ( ) and < > are all brackets. We know what we're talking about, even if everyone else is confused!

---
Marcus
better questions get better answers - faq581-3339
accessible web design - zioncore.com
 
I think it's perfectly acceptable to call () brackets - we were taught that at school, and I don't know anyone 'over here' who doesn't call them brackets.

Saying categorically that 'they are not brackets' is just plain wrong.

Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
I think I'll keep calling them parentheses. I think having different names for parentheses, brackets and braces is a "good thing", and helps to keep things clear and precise, especially when it comes to coding, where things like that DO matter. It's much simpler than calling them round brackets, square brackets and curly brackets.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top