我想使用MVC表单进行验证

时间:2015-04-16 19:28:30

标签: javascript jquery asp.net-mvc forms validation

我有一个html表单,当我更改radiobuttons时有输入。这些无线电按钮具有空壳的验证。例如,我检查了invoiceTypePerson和     填写其输入,然后单击“提交”按钮,但由于其他单选按钮的输入为空,表单不会提交。我想彼此独立提交。     我该怎么做?我的代码如下。

<div class="inputs">
    @Html.RadioButtonFor(model => model.InvoiceType, InvoiceType.Person, new { id = "invoiceTypePerson" })
    <label for="invoiceTypePerson" class="companyType">@T("RadioButton1")</label>
    @Html.RadioButtonFor(model => model.InvoiceType, InvoiceType.Company, new { id = "invoiceTypeCompany" })
    <label for="invoiceTypeCompany" class="companyType">@T("RadioButton2")</label>
</div>

<div class="inputs sirket">
    @Html.TextBoxFor(model => model.TaxOffice, new { @class = "text-box single-line", required = "required" })
    @Html.RequiredHint()
    <span class="taxD"></span>
</div>

<div class="inputs sahis">
    @Html.TextBoxFor(model => model.TcNumber, new { @class = "text-box single-line", required = "required" })
    @Html.RequiredHint()
    <span class="tc"></span>
</div>

<button class="add-address-button"></button>

if($("input[id='invoiceTypePerson']").is(":checked")){
    $('.add-address-button').click(function(){
            var tc = $('input[id*="Address_TcNumber"]').attr('value');
            if (!tc) {
                $("span.tc").html('TC is required');
                return false;
            }
            else if(tc){
                $("span.tc").html('');
            }

            var taxoffice = $('input[id*="Address_TaxOffice"]').attr('value');
            if (!taxoffice) {
                $("span.taxD").html('Taxoffice is required');
                return false;
            }
            else if(tc){
                $("span.taxD").html('');
            }           
    });
}

0 个答案:

没有答案