0

Is this possible to do this in Javascript and how?

function MyClick(){
ValidateTime(sender, args);  // what is the right way to call it?
}

function ValidateTime(sender, args) { //sender and args;these arguments are from a validator control
}

I need for MyClick to call that ClientValidationFunction(ValidateTime).

Adil Shaikh
  • 44,509
  • 17
  • 89
  • 111
user2356029
  • 1
  • 1
  • 1
  • Where is the definition of the `ClientValidationFunction()` function? Within your `MyClick()` when you call `ValidateTime()` you'd need to set the arguments to variables/values that are actually available at the time, but we can't advise you what they should be because you've shown an empty `ValidateTime()` definition that doesn't make it clear what the arguments are for. – nnnnnn May 06 '13 at 20:43
  • I want to use a checkbox to call the the ValidateTime function: – user2356029 May 06 '13 at 20:47
  • It is an attribution inside – user2356029 May 06 '13 at 20:49
  • function ValidateTime(sender, args) {args.isValid = true; errorValidator.isvalid = true;} I am only using the args to check if the control that this control is validating is being fill. Right now for this sample I am just setting it up to true all the time. – user2356029 May 06 '13 at 20:52

1 Answers1

0

The arguments are passed automatically to your ClientValidationFunction, you just have to define it with two parameters:

a validator:

<asp:CustomValidator id="CustomValidator1"
       ClientValidationFunction="ValidateTime"
       .....
       runat="server"/>

Here's the function

<script language="javascript"> 
   function ValidateTime(sender, args)
   {
        if (bla...){
            arguments.IsValid = true;
        } else {
            arguments.IsValid = false;
        }
   }
</script>

If you want to use the validation function from elsewhere i would extract the main logic into a separate function which you can use from your button-click handler and the ClientValidationFunction.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • I do have a checkbox and I want for the checkbox to call the ValidateTime(sender, args). Right now the ValidateTime is hooked up with a textbox. – user2356029 May 06 '13 at 20:56
  • @user2356029: So why don't you use two `CustomValidators`, one for the `TextBox` and one for the `CheckBox` with the same `ClientValidationFunction`. Or you can use one validator for both since the `CustomValidator` is the only validator that allows to omit the `ControlToValidate`. – Tim Schmelter May 06 '13 at 21:00
  • Could you exaplain me how can I attach a validator to my checkbox. Javascript: function CheckItem(sender, args) {} – user2356029 May 07 '13 at 12:46
  • If you want to validate multiple controls (like your TextBox+CheckBox) with a single `CustomValidator` you have to omit the `ControlToValidate` property. Then you have to get the reference to your controls via javascript in the `ClientValidationFunction`. For example with `var checkbox = document.getElementById("<%=ChkOvernight.ClientID %>");` – Tim Schmelter May 07 '13 at 12:51
  • hmmm... good point Tim, but i have more validation going on in my code, it will be a little difficult to manage all of them. What about trying to create a CustomValidator for my checkbox like you commented. I tried to use the argument"ControlTovalidate" but it didn't work. The function that I am calling from the CustomValidator is not Firing when I check the checkbox... How can I tie this CustomValidator to my checkbox? – user2356029 May 07 '13 at 13:37
  • After researching ..a Checkbox CAN'T have a CustomValidator, Does someone know how to do this? thanks – user2356029 May 07 '13 at 14:45
  • @user2356029: Every control can use a `CustomValidator` since that's the only validator which doesn't depend on controls at all (it's the only validator that allows to omit the `ControlToValidate`). – Tim Schmelter May 07 '13 at 14:46
  • how can I tied my checkbox to a Customvalidatod? – user2356029 May 07 '13 at 15:04
  • You don't need to "tie" the `CheckBox`, just use the `CustomValidator` without `ControlToValidate`. Here's an example http://stackoverflow.com/questions/1228112/how-do-i-make-a-checkbox-required-on-an-asp-net-form (however, you don't need `jQuery` as in that answer, just use `document.getElementById('id').checked` as shown above). – Tim Schmelter May 07 '13 at 15:07