自定义jquery验证器无法在iPad上运行

时间:2012-11-18 20:53:49

标签: jquery validation datetime

我有一个特定日期控件的自定义验证器。该控件为日,月和年提供三个下拉列表。

该功能适用​​于Firefox,Chrome和IE9 / 10。它在iPad浏览器上失败了。我尝试使用Adbode Edge Inspect进行调试,但无法调试javascript。

这是我的代码。有什么建议吗?

$.validator.addMethod("validcustomdateday", function (value, element) {

    var elementName = $(element).attr("Id");
    var day = value.length === 1 ? "0" + value : value;
    var month = $("#" + elementName.substr(0, elementName.lastIndexOf("_")) + "_Month").val();
    month = month.length === 1 ? "0" + month : month;
    var year = $("#" + elementName.substr(0, elementName.lastIndexOf("_")) + "_Year").val();

    var dateStr = year + "-" + month + "-" + day;
    return !isNaN(Date.parse(dateStr));

});

以下是渲染的控件

<div class="control-group offset2 span6">
                <label class="control-label" for="Solicitors___0__DateOfBirth">Fecha de Nacimiento</label>
                <div class="controls">

<input id="Solicitors___0__DateOfBirth_DateOfBirth" name="Solicitors()[0].DateOfBirth.DateOfBirth" type="hidden" value="">
<select class="input-mini" data-bind="value: Solicitors()[0].DateOfBirth.Day" data-val="true" data-val-number="The field Dia must be a number." data-val-required="Debe especificar el Día" data-val-validcustomdateday="Se debe especificar una fecha válida" id="Solicitors___0__DateOfBirth_Day" name="Solicitors()[0].DateOfBirth.Day" placeholder="Dia"><option selected="selected">1</option>
<option>2</option>
<!-- Other days go here -->
<option>31</option>
</select>
<select class="input-small" data-bind="value: Solicitors()[0].DateOfBirth.Month" data-val="true" data-val-number="The field Mes must be a number." data-val-required="Debe especificar el Mes" id="Solicitors___0__DateOfBirth_Month" name="Solicitors()[0].DateOfBirth.Month"><option selected="selected" value="1">Enero</option>
<option value="2">Febrero</option>
<option value="3">Marzo</option>
<option value="4">Abril</option>
<option value="5">Mayo</option>
<option value="6">Junio</option>
<option value="7">Julio</option>
<option value="8">Agosto</option>
<option value="9">Septiembre</option>
<option value="10">Octubre</option>
<option value="11">Noviembre</option>
<option value="12">Diciembre</option>
</select>
<select class="span1" data-bind="value: Solicitors()[0].DateOfBirth.Year" data-val="true" data-val-number="The field Año must be a number." data-val-required="Debe especificar el Año" id="Solicitors___0__DateOfBirth_Year" name="Solicitors()[0].DateOfBirth.Year"><option value="1993">1993</option>
<option value="1992">1992</option>
<!-- Full Year List goes here -->
<option value="1937">1937</option>
</select>
<span class="help-inline field-validation-valid" data-valmsg-for="Solicitors()[0].DateOfBirth.Day" data-valmsg-replace="true"></span>
<span class="help-inline field-validation-valid" data-valmsg-for="Solicitors()[0].DateOfBirth.Month" data-valmsg-replace="true"></span>
<span class="help-inline field-validation-valid" data-valmsg-for="Solicitors()[0].DateOfBirth.Year" data-valmsg-replace="true"></span>
                    <span class="field-validation-valid help-inline" data-valmsg-for="Solicitors()[0].DateOfBirth" data-valmsg-replace="true"></span>
                </div>
            </div>

1 个答案:

答案 0 :(得分:0)

通过将Date.parse(“1993-01-01”)更改为Date.parse(“1993/01/01”)来解决此问题。所有Safari浏览器都无法将第一个字符串识别为有效日期