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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A little help with javascript if/then...

Status
Not open for further replies.

shadowspell

Programmer
Nov 24, 2004
6
US
A little help with javascript please...
Hi all, me again,

Could someone help me with this two function javascript?
In the first function, I want to check the value of a form field called "proj_sel_wrtype". It is a radio button field that will be equal to either "Posting" or "Project".

If the value is "Posting" then; check to see if another field, "proj_sel_posting_type" contains a value; if it does not, then throw up a javascript alert. The value is selected from a drop down menu.

In the second function, if "proj_sel_wrtype" is equal to "Project" then; check to see if a third field, "proj_sel_activity"

contains a value; if it does not, then throw up a javascript alert. The value is selected from a drop down menu.

I call the js functions from the form submit button using the onClick event. The problem is that if either "proj_sel_posting_type" or "proj_sel_activity" is left blank the form gets processed anyway. Here is the code: - - - - -

<script language="javascript">
function evaluate_1(wr1)
{
if (icsworkrequest.proj_sel_wrtype.value == "Posting")
{
if (icsworkrequest.proj_sel_posting_type == "")
{
alert("For POSTINGS, select POSTING TYPE. \n Click OK to contine.");
icsworkrequest.proj_sel_posting_type.focus();
return false;
}
}
return true;
}

function evaluate_2(wr2)
{
if (icsworkrequest.proj_sel_wrtype.value == "Project")
{
if (icsworkrequest.proj_sel_activity == "")
{
alert("For PROJECTS, select PROJECT TYPE. \n Click OK to contine.")
icsworkrequest.proj_sel_activity.focus();
return false;
}
}
return true;
}
</script>
---------------------------------
In the submit button I have,
"onClick = "return evaluate_1(icsworkrequest); return evaluate_2(icsworkrequest)"

'icsworkrequest' is the name given to the form, and I realize I'm no good at javascript.
Thanks for any help,
Chris
~`^%
 
Code:
// We're going to make another function that
// gets the selected value in a group of
// radiobuttons because .value doesn't do it
function getSelectedValue(radios)
{
  for (var i=0; i<radios.length; i++) // Loop through the buttons
  {
    if (radios[i].checked) // If this one is checked
      return radios[i].value; // return its value
  }
  return ""; // Return "" if none are checked
}

function evaluate(wr) // Combine them into one function
{
  var radioValue = getSelectedValue(wr1.element['proj_sel_wrtype']);
  // Store the selected value. Refer using .element[''] for
  // greater compatibility
  if (radioValue == "Posting") // if Posting is selected
  {
    if (wrt.element['proj_sel_posting_type'].selectedIndex == -1) // If nothing is selected 
    {
      alert("For POSTINGS, select POSTING TYPE. \n Click OK to contine.");
      // Alert the error message
      wr1.elements['proj_sel_posting_type'].focus();
      // Focus the element
      return false;
    }
  }
  else if (radioValue == "Project")
  // if Project is chosen
  {
    if (wr.elements['proj_sel_activity'] == "")
    // if proj_sel_activity is blank
    {
      alert("For PROJECTS, select PROJECT TYPE. \n Click OK to contine.");
      // Alert error message
      wr.elements['proj_sel_activity']y.focus();
      // focus the element
      return false;
    }
  }
  // If a value hasn't been returned yet, return true
  return true;
}
Call using
Code:
<input type="submit" value="Submit" onclick="return calculate(this.form);">
<!-- or -->
<form name="icsworkrequest" id="icsworkrequest" method="post" action="page.php" onsubmit="return calculate(this);">

--Chessbot

"Violence is the last refuge of the incompetent." -- Asimov, Foundation
 
Chessbot,

My my you seem to be real good at this. You seem to have an understanding of my vision. Thank you for your feedback and expertise. I will test this code after the Holidays and provide feedback. Happy Holidays to you and yours.
Chris
~`^%
 
Chessbot,
This worked out great. Thanks for your time and expertise. I hope I can actually contribute one day.
Take care,
Chris
~`^%
 
Great!

--Chessbot

There is a level of Hell reserved for probability theorists in which every monkey that types on a typewriter produces a Shakespearean sonnet.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top