Skip to main content
Participating Frequently
March 5, 2008
Question

Button help again

  • March 5, 2008
  • 1 reply
  • 362 views
I know this is probably and html and java question, but since it is part of a cf app, I thought I would post here.

I have this and input field and checbox next to it. If the checkbox is not checked, then the input field is required. If the checkbox is check, then the input field is optional. Right now, the onvalidate will make it required all the time.

Can someone show me the javascript to validate (required) the input field only if the checkbox is not checked, and if the checkbox is checked, then it is optional ?

thanks


<cfinput type="text" name="return_to_supplier_supplier_number" onValidate="validateOptionTextBox" message="Supplier number is required" value="">
<cfinput type="checkbox" name="not_supplier" value="NRS"> Not a Supplier
    This topic has been closed for replies.

    1 reply

    Astonished_protector15C3
    Participating Frequently
    March 6, 2008
    Hi Trojan Fan,

    I think we have to handle this through javascript
    Please try the following code

    <script>
    function fn_validate()
    {
    if(form1.not_supplier.checked==false)
    {
    alert("Supplier number is required");
    return false;
    }
    return true;
    }
    </script>

    <cfform name="form1" onsubmit="return fn_validate();">
    <cfinput type="text" name="return_to_supplier_supplier_number" value="">
    <cfinput type="checkbox" name="not_supplier" value="NRS"> Not a Supplier
    </cfform>

    Hope this will works for you
    Inspiring
    March 6, 2008
    Hello V.K.R.,

    Thanks for your response.

    Tere is your code that I copied and pasted, and I just added the focus part. But what is happening is it does validate and then display the error, but it never stops and continues to go to the next screen.

    Can you tell me what is wrong ? Thanks

    function validateForm(form){

    var returnStatus = true;

    {
    if (form.not_supplier.checked == false)
    {
    alert("Supplier number is required");
    form.return_to_supplier_supplier_number.focus();
    return false;
    }
    return true;
    }