0

The application is a Web MVC application in C#. When the user is editing a record and clicks on a checkbox I want to make sure 3 fields are filled in.

On the controller I have created a clientside script which is called checkfields:

checkBoxProperties.ClientSideEvents.CheckedChanged = 
    "function (s,e) { checkfields(); }"; 

On the CSHTML I have

function checkfields() 
{
    alert("Value Empty");
    return false;

}

When I check the checkbox I am getting the alert pop up but how do I reference the model fields and popup alert if one of the fields are empty.

Zoran Horvat
  • 10,924
  • 3
  • 31
  • 43
Steve Moro
  • 83
  • 7
  • have you considered using unobtrusive validation? – Daniel A. White Feb 17 '17 at 15:56
  • Is there syntax with unobtrusive validation where you can make a field required with an if condition. For example if they select the checkbox where it indicates range and then there is a text box where Range1 and another TextBox where Range2 needs to be filled in. I am using this validation to make model fields required but do not know if you can do this based on another field. – Steve Moro Feb 17 '17 at 17:48
  • Use a [foolproof](http://foolproof.codeplex.com/) `[RequiredIfTrue]` or similar conditional validation attribute applied to your properties so that you get both client and server side validation. –  Feb 17 '17 at 22:15

1 Answers1

0

You can just use JQuery to select each checkbox and query if it has been checked.

How to check whether a checkbox is checked in jQuery?

$("#myCheckBox").on("click", function () {
if ($("#myCheckBox").is(':checked')) {

Now you can show a hidden message (show a hidden label, for example), do a popup, or whatever.

Community
  • 1
  • 1
Gary Wolfe
  • 91
  • 3
  • That would work to trigger function when RangeCheckBox is clicked but if they click that I need to make sure Range1 and Range2 are not null or else the user need to enter those values before they enable Range. – Steve Moro Feb 17 '17 at 17:59