jquery梳理字段以进行验证

时间:2014-11-18 19:14:19

标签: javascript jquery html validation

我的网页有3个选择框,用户可以在其中选择日,月和年 当页面加载时,它显示" Day"在当天选择选项,月份的月份名称(例如1月):



    $("#join").validator.addMethod('check_dob', function(value, element, params) {
        //unsure about this logic
    }, "Enter the number of persons (including yourself)");    
    
    
 		$("#join").validate({
 			rules : {
 				firstname: "required",
 				lastname: "required",
 				dob: { check_dob: ['day', 'month', 'year'] }
 			},
 			
 			messages: {
 				firstname: "Firstname is required",
 				lastname: "Lastname is required",
 	    }
    
 		});

<label for="dob">Date of Birth:</label>

<!-- day -->
<select name="day" id="day">
<option value='0'>Day</option><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option><option value='6'>6</option><option value='7'>7</option><option value='8'>8</option><option value='9'>9</option><option value='10'>10</option><option value='11'>11</option><option value='12'>12</option><option value='13'>13</option><option value='14'>14</option><option value='15'>15</option><option value='16'>16</option><option value='17'>17</option><option value='18'>18</option></select>

<!-- month -->
<select name="month" id="month">
<option value="0">Month</option>
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>									
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>

<!-- year -->
<select name="year" id="year">
<option value='0'>Year</option><option value='1996'>1996</option><option value='1995'>1995</option><option value='1994'>1994</option><option value='1993'>1993</option><option value='1992'>1992</option><option value='1991'>1991</option><option value='1990'>1990</option><option value='1989'>1989</option><option value='1988'>1988</option><option value='1987'>1987</option><option value='1986'>1986</option><option value='1985'>1985</option><option value='1984'>1984</option><option value='1983'>1983</option><option value='1982'>1982</option><option value='1981'>1981</option></select> 									
&#13;
&#13;
&#13;

当表单提交时我希望表单验证,如果用户没有选择日,月,年,他将收到错误消息。这是1条错误消息,而不是3.我应该在年份输入字段的右侧显示(因此它与其他验证错误消息一致)

不确定如何对此进行编码,谷歌搜索和阅读http://jsfiddle.net/hBXL6/,但它对我没有用。任何指导都会很棒

1 个答案:

答案 0 :(得分:0)

使用jQuery我会删除你在那里的东西,并进行更简单的选项检查,因为基于值.val()将工作得很好;我已经组装了一个jsfiddle,它符合我的想法。 (我希望)你可以在这里找到:http://jsfiddle.net/dhershman/33pv91am/

    var day = $('#day option:selected').val();
    var mnth = $('#month option:selected').val();
    var year = $('#year option:selected').val();

它将查看您的选项设置,查看某个值是否等于零(您也可以将此值设置为小于或等于负数,无论出于何种原因)然后抛出您的错误,否则它将注册为正确。