Santosh
Santosh

Reputation: 2495

validating multiple controls in javascript

I am adding multiple controls on an .aspx page from the .vb page based on certain conditions.

My code looks like following:

    Dim sb As New StringBuilder

    sb.Append("<table border='0'cellpadding='0' cellspacing='0' width='50%' class ='tabledata' id='tblContent'>")
    For Each item As myObject In myLst            
        sb.Append("<tr><td style='width:50%;' valign='top'>")
        sb.Append("<textarea id=txt_comments" & i & " name='txt_comments' rows='5' cols='60'></textarea></td>")
        sb.Append("<td style='width:15%' valign='top' align='center'><select ID = validate" & i & " name=ValidateValues style ='border:1;width:150px'><option value = ''>Select</option><option value = 'Yes'>Yes</option><option value = 'No'>No</option><br /><br /></td>")
                    sb.Append("</tr><tr>")
    Next
    sb.Append("</table>")
    myContent.InnerHtml = sb.ToString

So here I am creating <textarea> and <select> dynamically and adding them to my div(myContent)

 <div id="structuredContent" runat="server">
 </div>

I have a button next where I need to validate for few conditions.

My validation rule is:

I am trying to validate like following:

function validateComments() {           
        var errorcheck = 0;
         $("[id^=txt_comments]").each(function () {
            var comment = $.trim($(this).val());
            $("[id^=validate]").each(function () {
                debugger;
                var value = $(this).val();

                if (comment == 0 && value == "Yes") {
                    debugger;
                    errorcheck = 1;
                }


            });


        });            if (errorcheck == 1) {
             //show error message
            }
        else {
            ErrorHide();
            return true;
        }

    }

I am able to validate only for one control(which is textarea) from the above code.

The textbox and respective dropdown should be validated along.

How do I add validation for dropdown and can I combine with in the same function.

Any help?

Thanks in advance.

Upvotes: 1

Views: 80

Answers (1)

Muhammad Usman
Muhammad Usman

Reputation: 10148

I don't know how do you expect this like if (comment == 0) { to work.

You'll always get a string as a value and checking it with 0 would always return false. Rather you need to check it with "".

And to enable/disable textarea you'll have to attach an event to select tag and do whatever you want to do.

here is an example

$("#d").change(function(){
   if($(this).val() === 'n'){
     $("#t").prop('disabled', 'disabled')
   }else{
    $("#t").prop('disabled', false)
   }

});
$('body').on('click', '#b', function() {
   var text = $.trim($("#t").val());
    if(text === "" && !$("#t").prop('disabled')){
     alert("yo! not valid") 
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<select id="d">
 <option value="0">Select</option>
 <option value="y">Yes</option>
 <option value="n">No</option>
</select>
<textarea maxlength="50" id="t"></textarea>\

<button id="b">Validate</button>

Upvotes: 1

Related Questions