开发者

Form shouldn't be submitted if Check box is not selected [duplicate]

开发者 https://www.devze.com 2023-03-07 00:56 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Recommendation for javascript form validation library
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Recommendation for javascript form validation library

In my HTML page there is a radio button, if that radio button is selected it will enable a div containing a textbox.

I need some validation here. If the checkbox is selected and textbox is empty then the submit button should not work and it should remain on the same page.

How can i implement this?

edited the code so that it will work now.

How can I implement this in Play! Framework? This is a part of my code

<div id="ask-poll">
    <form name="myform" action="@{Application.askquestion()}" onsubmit="return validateThisForm();">
    <div class="wf-group">
      <p><label for="questionText"><h3>Who d开发者_开发知识库o you want to ask for their opinion?</h3></label></p>


While this isnt a coded answer, there are a number of form validation javascript examples (hence Im not going to insult you by posting a bunch of links to them) where you can see how they use javascript to test if all the required elements are completed, eg, various "required" textboxes, a "have you read this" checkbox etc. You then implement this as part of the submit "onclick" and if its not valid you return "false" this prevents the form being submitted.


This has been taken from @danyolgiax's link

function theChecker()
{ 
if(document.theForm.theCheck.checked==false)
{
document.theForm.theButton.disabled=true;
}
else
{
document.theForm.theButton.disabled=false;
}
}
</script>


In case You want to use jquery here is a sample code :

$(document).ready(function()
{
    $("#somediv").hide(); //hide div
    $('input[type=submit]').attr("disabled","disabled"); //disable submit button
    function checkAll()  //prepare function for disabling / enabling submit button
    {
        if($('input[type=text]').val() == '')
        {
            $('input[type=submit]').attr("disabled","disabled");
        }
        else
        {
            $('input[type=submit]').removeAttr('disabled');
        }
    }
    $('input[type=text]').bind('mouseenter mouseleave focus blur keypress', function() {
        checkAll();
    });  //bind function for text input, checking if its empty or not and calling checkAll() function

    $('input:radio')click(function() // bind function for radio that will show div after clicking
    {
        $("#somediv").show();
    });
}
0

精彩评论

暂无评论...
验证码 换一张
取 消