0

I am taking the reference of This question and validating-several-fields-with-the-same-name for validating the page inputs. The difference between validating-several-fields-with-the-same-name and My question is validating-several-fields-with-the-same-name will validate all the inputs of the page whereas I have the fields which do not require validation some of the inputs are optional in my case. I have dynamic inputs like dorpdown, textbox, radiobutton, checkBox etc and static inputs as well. I am generating html for inputs from C# code behind like this :

      if (dt.Rows[i]["IsRequired"].ToString() == "True")
      {
           sb.Append("<tr id='tr" + dt.Rows[i]["PropertyID"].ToString() + "' style=' width:100%'><td  style='width:30%; text-align:right'>&nbsp;&nbsp;&nbsp;" + dt.Rows[i]["PropertyName"].ToString() + "</td>" + "<td style='width:70%; text-align:left'> <select id='" + dt.Rows[i]["PropertyID"].ToString() + "_DropDown' class='dynamicControls dropDown RequiredFields' name='RequiredFields' ctrl='select'>");
      }
      else
      {
           sb.Append("<tr id='tr" + dt.Rows[i]["PropertyID"].ToString() + "' style=' width:100%'><td  style='width:30%; text-align:right'>&nbsp;&nbsp;&nbsp;" + dt.Rows[i]["PropertyName"].ToString() + "</td>" + "<td style='width:70%; text-align:left'> <select id='" + dt.Rows[i]["PropertyID"].ToString() + "_DropDown' class='dynamicControls dropDown' ctrl='select'>");
      }

now I am validating the inputs like this

       $(fromID).validate({
         rules: {.....validating for name}
       });
       $('.RequiredFields').each(function () {
            $(this).rules('add', {
            selectNone: true,
            messages: {
            required: "<span  style='color:#DD4B39;padding-left:10px; font-size:smaller;'>This field is required </span>"
            }
         });
       });

I have tried to add the class in place of name to validate the inputs but unable to validate. I also tried this as well

         'Price':
        {
            number: true
        },
        '.RequiredFields': {
            required: true
        }

I tried this piece of code after validating the name

     $.validator.addClassRules("EmailValidation", {
            email: true,
            required: true,
            messages: {
                email: "<span  style='color:#DD4B39;padding-left:10px; font-size:smaller;'>Invalid e-mail input</span>",
                required: "<span  style='color:#DD4B39;padding-left:10px; font-size:smaller;'>This field is required</span>"
            }
        });

It is validating but after giving inputs also it is validating. I tried this as well. Is there any other way to validate the inputs from jquery?

Community
  • 1
  • 1
Iswar
  • 2,211
  • 11
  • 40
  • 65
  • 1
    **Nothing will work until your inputs each have a unique `name`.** There is no workaround for this, as it's how the plugin keeps track of everything. – Sparky Jun 20 '14 at 14:22
  • Also see this: http://stackoverflow.com/a/16658340/594235 and this: http://stackoverflow.com/a/15745248/594235 – Sparky Jun 20 '14 at 14:24

0 Answers0