1

I have one textbox and two checkboxes with a submit button. I want to put a customvalidator which shall call a javascript for the following condition -

If textbox.value > 0 then checkbox1 = checked or checkbox2 = checked. If neither checkboxes checked when textbox.value > 0 then raise error.

any ideas?

oksdd
  • 11
  • 1
  • 1
    you might want to clarify what logic you actually want, as that doesnt make any sense – Andrew Bullock Jul 12 '10 at 15:47
  • what part of it doesnt make sense? its basically when textbox has some text in it and the user clicks submit, at least one of the checkboxes should be checked. I dont want user to submit with no checkboxes checked. thats all – oksdd Jul 12 '10 at 16:00

1 Answers1

2

This is what I'm assuming you want:

function validate()
{
  var t = document.getElementByID( "myTextbox" );
  var c1 = document.getElementByID( "myCheckbox1" );
  var c2 = document.getElementByID( "myCheckbox2" );

  if( t.value != "" )
  {
    if( !(c1.checked || c2.checked) )
    {
      alert( "You didn't do what I wanted you to do. Bad user!" );
      return( false );
    }
  }

  return( true );
}

and

<form method=post action=blah onsubmit="JavaScript: return( validate() );">
  <input type=text id=myTextbox name=blah><br>
  <input type=checkbox id=myCheckBox1 name=blah><br>
  <input type=checkbox id=myCheckBox2 name=blah><br>
  <input type=submit>
</form>
Sparafusile
  • 4,696
  • 7
  • 34
  • 57