当前位置: 动力学知识库 > 问答 > 编程问答 >

asp.net mvc - validation for either or dynamic textbox in mvc3 razor?

问题描述:

I have a form that form loads a partial view when i select the dropdown values.Here the partialview loads two dynamic textbox with different ID values.

@model List<DataBaseModel.OrderQuoteTBvalues>

@{

ViewBag.Title = "_ingroundDynamic";

}

@if (Model.Count() > 0)

{

<table>

<tr>

<td valign="top">

<div>

<table border="0" cellpadding="3" cellspacing="3" width="20%">

<tr style="background-color: #808080; color: #fff; font-size: 14px;">

<th align="left" width="100px">Pool Shape Type</th>

<th align="center" width="20px">Feet<br>

<th align="left" width="20px">Inch</th>

</tr>

@foreach (var item in Model)

{

<tr>

<td>@item.Shape_type</td>

<td>

<input style="width: 100px;" type="text" onkeydown="Integerkeydown(event)" id="PF:@item.Shape_type" name="PF:@item.Shape_type" /></td>

<td>

<input style="width: 100px;" type="text" onkeydown="Numerickeydown(event)" onchange="PIChange(this)" id="PI:@item.Shape_type" name="PI:@item.Shape_type" maxlength="4" /></td>

</tr>

}

}

Here the model is as follows:

 public class OrderQuoteTBvalues

{

public string Shape_type { get; set; }

}

After that i have placed the next button onSubmit. if i click the Next button means i need to validate, either or textbox contains value. if it contains the value the validation true else false.

How to achieve that. Need to write any Custom Validation Method?.. If i give class="required" means all textbox is validated. but i need either or textbox to be validated.? Please help me to solve this issue. Thank you so much.

网友答案:

Try below Jquery

 $("#validate").live({ 
        click: function() {

       var rows = $("#your_table tr:gt(0)"); // your_table is id of you table---gt(0) to skip first row
       var i=true;

                rows.each(function(index) {
        var firsttext = $("td:nth-child(1) input", this).val();
        var secondtext = $("td:nth-child(2) input", this).val();
                   if(firsttext=="" && secondtext=="")
                       i=false;
            });
   if(i==true)
      alert('Valid'); 
            else
                alert("Invalid")
        }
    });

Child differ according to your order so correct that Hope it helps leave comment if not understood

网友答案:
$("#btnNext").click( function() {
var valid = false;

   if($("#table tr").length > 0) {

    var textbox1 = $("table tr td:nth-child(1) input", this).val();
    var textbox2 = $("table tr td:nth-child(2) input", this).val();

    if(textbox1=="" && textbox2==""){
      valid = false;
     }
    else if(textbox1 != "" && textbox2 != "") {
     valid = false;
    }
    else {
     valid = true;
     }
  }

});

网友答案:

the output will be like this friends.

分享给朋友:
您可能感兴趣的文章:
随机阅读: