0

I've got two DropDownLists in a form on a page that contain the same values (a list of languages). I want to ensure that the user doesn't select the same value in each of the drop downs.

I've tried using JavaScript to ensure the selected values aren't the same, it works fine but the form submits anyway.

What's the best way to accomplish this?

Here's the code from my View:

    <script type="text/javascript">

    function CheckLanguageDDL() 
    {
        var form = document.getElementById("form0");

        var sourceLangIndex = form.SourceLanguage.selectedIndex;
        var targetLangIndex = form.TargetLanguage.selectedIndex;
        var strSourceLanguage = form.SourceLanguage.options[sourceLangIndex].text;
        var strTargetLanguage = form.TargetLanguage.options[targetLangIndex].text;

        if (strSourceLanguage == strTargetLanguage) 
        {
            alert("Source Language and Target Language must be different!");
            return;

        }
    }

</script>

    <% Html.BeginForm("Index", "Translate", FormMethod.Post, new { enctype = "multipart/form-data" }); %>

    <fieldset>
    <legend>Request</legend>

    <br />
    <div class="editor-label">
        <%: Html.LabelFor(m => m.SourceLanguage) %>:
     </div>
    <div class="editor-field">
        <%: Html.DropDownList("SourceLanguage", (IEnumerable<SelectListItem>)ViewData["SourceLanguages"]) %>
        <%: Html.ValidationMessageFor(m => m.SourceLanguage) %>
    </div>

    <br />
    <div class="editor-label">
        <%: Html.LabelFor(m => m.TargetLanguage) %>:
     </div>
    <div class="editor-field">
        <%: Html.DropDownList("TargetLanguage", (IEnumerable<SelectListItem>)ViewData["TargetLanguages"]) %>
        <%: Html.ValidationMessageFor(m => m.TargetLanguage) %>
    </div>

    <input type="submit" value="Submit Request" onclick="CheckLanguageDDL();" />
    </p>
    </fieldset>

Thanks.

3
  • 1
    It obviously isn't working fine. Can you post your code? How are you canceling the submit when the form doesn't validate? Commented Jan 25, 2011 at 20:31
  • 1
    How are you calling the CheckLanguageDDL function? Commented Jan 25, 2011 at 20:37
  • Apologies - missed some code during copy/paste. Commented Jan 25, 2011 at 20:39

1 Answer 1

1

Make the function return a true/false value that the form submit use that return value

function CheckLanguageDDL() 
{
    var form = document.getElementById("form0");

    var sourceLangIndex = form.SourceLanguage.selectedIndex;
    var targetLangIndex = form.TargetLanguage.selectedIndex;
    var strSourceLanguage = form.SourceLanguage.options[sourceLangIndex].text;
    var strTargetLanguage = form.TargetLanguage.options[targetLangIndex].text;

    if (strSourceLanguage == strTargetLanguage) 
    {
        return false;
    }

    return true;
}

And on the button:

onclick="return CheckLanguageDDL();"
Sign up to request clarification or add additional context in comments.

2 Comments

It works - thanks. That's one of the quickest resolutons to a problem I've ever got!
@Jimmy C - Glad to have helped :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.