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
- Iterate through all span elements from a certain class
- Check if the
IsRequired
hidden input field is set to true - 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!";
}
});