0

I am working on an asp.net mvc3 application. I use strongly-typed razor view and I have multiple hidden input fields to hold the data I need. To perform client-side validation I need to check if the textbox field is required, data which is held in a hidden input, and if the texbox value is required I need to perform the check.

So logically I think there are three steps which I need to make

  1. Iterate through all span elements from a certain class
  2. Check if the IsRequired hidden input field is set to true
  3. If the above is true, then check the value of the textbox

So far so good. I made several attempts to do this, but it seems to be much higher than my jQuery abilities that's why I'm seeking help here.

Here is the HTML generated for one of the spans. The code for the other spans is similar only the classes are different :

<span class="checkFloat">
Value before :
<br>

<input class="text-box single-line" type="text" value="" name="[29].FieldValue">

<input type="hidden" value="118" name="[29].Id" data-val-required="The Id field is required." data-val-number="The field Id must be a number." data-val="true">

<input type="hidden" value="RowId_6" name="[29].DocRowId" data-val-required="The DocRowId field is required." data-val-length-max="40" data-val-length="The field DocRowId must be a string with a maximum length of 40." data-val="true">

<input type="hidden" value="True" name="[29].IsRequired" data-val-required="The IsRequired field is required." data-val="true">

<input type="hidden" value="3" name="[29].ColumnNo" data-val-required="The ColumnNo field is required." data-val-number="The field ColumnNo must be a number." data-val="true">

<input type="hidden" value="6" name="[29].RowNo" data-val-required="The RowNo field is required." data-val-number="The field RowNo must be a number." data-val="true">

</span>

So what I need is to iterate through all span elements like this -

<span class="checkFloat"> for each span I need to check the value in the input - <input type="hidden" value="True" name="[29].IsRequired" data-val-required="The IsRequired field is required." data-val="true">

And if the value is true like in this case I need to check the value of -

<input class="text-box single-line" type="text" value="" name="[29].FieldValue">

I made a lot of attempts to do it myself but I don't think I've been even close, but cause I know it's much better when people see that one have been put some effort into the problem he's asking for, this is my last attempt to try and get the value of the textbox :

$(".checkULong input[type='hidden'][value='True'][name$='IsRequired']").each(function () {
            alert($(this).val());
            if (!mathFunctions.isInt($(this).val())) {
                $(this).focus();
                throw "The filed can not be empty!";
            }
});
4

1 回答 1

1

似乎您正在以某种方式使用 jQuery 不显眼的验证,但此时,您正在为验证工作制定解决方法。我认为,一个好主意是使用本主题中描述的条件验证(尽管可能有点过时)。RequiredIf基本上,你用特殊的“ ”属性来装饰你的模型。然后,编写一个 javascript 验证适配器,为客户端准备验证规则。(所需步骤之一可能是实施IClientValidatable)。我知道这远非您的案例的解决方案,但它可能会为您指明正确的方向。

于 2013-06-19T10:52:34.453 回答