Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7659695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:21:59+00:00 2026-05-31T13:21:59+00:00

I am validating two controls from a single onclick. Both controls are mandatory. Onclick

  • 0

I am validating two controls from a single onclick. Both controls are mandatory.

Onclick calls the validateLocation function, which, if valid, then calls the validateForm function.

My problem:

If the user selects a state and clicks submit, the Javascript works as expected, perfect.

BUT

If the user selects a seminar location but no state, the javascript will fire, but the selection already made for the location will be cleared. I need that selection to stay selected, so users don’t have to repeat clicks.

My Question:

How to I retain the values if only one of the two criteria are met?

Code below:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>

<script type ="text/javascript" language="javascript">
<!--
function validateLocation() {
var radios = document.getElementsByName('RadioGroup1')

for (var i = 0; i < radios.length; i++) {
    if (radios[i].checked) {
    validateForm();
    return true;
    }
};
// not checked, show error
document.getElementById('ValidationError').innerHTML = 'Required';
alert("Please select a seminar location");
return false;
}
function validateForm() {
if(document.form1.State.selectedIndex==0)
{
alert("Please select a State");
document.form1.State.focus();
return false;
}
return true;
};
-->
</script>
</head>

<body>
<div>
<form name="form1" onSubmit="return validateLocation();">
<div>
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td style="text-align:right">state:&nbsp;</td>
<td>    
    <select name="State" id="State">
        <option value="">Select</option>
        <option value="AK">AK</option>
        <option value="AL">AL</option>
        <option value="AR">AR</option>
    </select>
</td>
</tr>
</table>
</div>
<div>
<table border="0" cellspacing="0" cellpadding="0">
<tr>
    <td colspan="2"><strong>Choose a seminar location</strong></td>
</tr>
<tr>
    <td valign="top">
      <input type="radio" name="RadioGroup1" value="Fayetteville Senior Center" id="RadioGroup1_0">
      </td>
    <td><strong>Location One</strong></td>
</tr>
<tr>
    <td valign="top">
      <input type="radio" name="RadioGroup1" value="Springdale Holiday Inn" id="RadioGroup1_1">
      </td>
    <td><strong>Location Two</strong></td>
</tr>
<tr>
    <td valign="top">
      <input type="radio" name="RadioGroup1" value="The Riordan Hall" id="RadioGroup1_2">
      </td>
    <td><strong>Location Three</strong></td>
</tr>
</table>
<div id="ValidationError" name="ValidationError"></div>
</div>
<div>
<input type="submit" name="button" id="button" value="Submit">
</div>
</form>
</div>
</body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:22:00+00:00Added an answer on May 31, 2026 at 1:22 pm

    I’ve slightly changed the script, to get it more logical:

    <script type ="text/javascript" language="javascript">
    function validateLocation() {
      var radios = document.getElementsByName('RadioGroup1')
    
      for (var i = 0; i < radios.length; i++) {
          if (radios[i].checked) {
            return true;
          }
      };
    
      // not checked, show error
      document.getElementById('ValidationError').innerHTML = 'Required';
      alert("Please select a seminar location");
      return false;
    }
    
    function validateState() {
      if(document.form1.State.selectedIndex==0)
      {
        alert("Please select a State");
        document.form1.State.focus();
        return false;
      }
      return true;  
    }
    
    function validateForm() {
      if (!validateState() || !validateLocation()) return false;
      return true;
    };
    </script>
    

    You just need to change the onSubmit handler in the form tag as well to:

    ...onSubmit="return validateForm();"...
    

    Then, the form will be validated, and not submitted, till both validations are successfully.

    Hope that helps,

    cheers,
    Daniel

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a nutshell, let's say I have two textboxes, both have RequiredFieldValidator controls. I
Two function should be implemented, and I am wondering what type of validation I
I have a form that calls on two separate models. My validation works correctly
For the ASP.NET validator controls, I want to use both client-side validation for the
I have two form classes inheriting from a common base. One of the forms
I have a web page with various controls. Two of them are dropdownlists. The
I have two validation groups and two validation summaries on my page. Controls belong
I have a ASP.NET MVC 3 view with esentially two forms, but which reside
I have two instances of a user control on a page. Both have fields
I have written a Sharepoint 2007 web part which requires validation in both the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.