我正在驗證表單。它正確地驗證所有領域,但是當我在文本字段中輸入正確的數據,那麼它仍然不處理數據並返回everything wrong
而我想它返回everything correct
驗證總是返回錯誤,即使它是正確的。爲什麼?
這裏是我的js代碼
var url, container, form,
View = {
init: function() {
container = $('.container');
$(document).on('submit', '#form', View.findView);
},
loadViews: function (view, flag, e) {
e.preventDefault();
var search = $('.search');
if(flag) {
if (View.validation.emptyFieldsValidation() == true) {
console.log('everything correct');
} else {
console.log('everything incorrect');
}
}
return false;
},
validation: {
emptyFieldsValidation: function() {
var inputs, field;
$('#form input').each(function (i, el) {
inputs = $(this);
if (inputs.val() == '') {
inputs.css('border', '1px solid red');
return false;
} else {
inputs.css('border', '1px solid #ccc');
if (inputs.hasClass('from')) {
console.log('from');
if (View.validation.validateAddress(inputs.val())) {
console.log('from correct address');
return true;
}
else {
inputs.css('border', '1px solid red');
console.log('from incorrect address');
//return false;
}
}
if (inputs.hasClass('to')) {
console.log('to');
if (View.validation.validateAddress(inputs.val())) {
console.log('to correct address');
//return true;
}
else {
inputs.css('border', '1px solid red');
console.log('to incorrect address');
//return false;
}
}
if (inputs.hasClass('time')) {
if (View.validation.validateForNumber(inputs.val())) {
console.log('time correct');
//return true;
}
else {
inputs.css('border', '1px solid red');
console.log('time incorrect');
//return false;
}
}
}
});
return false;
},
validateAddress: function (val) {
var streetregex = /^[\w+\s]+\d+,\s*[\s\w]+$/;
if (streetregex.test(val)) return true;
else return false;
},
validateForNumber: function (val) {
if (!isNaN(val)) return true;
else return false;
}
}
};
View.init();
這裏是我的html代碼
<section class="search">
<form id="form" action="">
<section>
<input type="text" name="from" class="from address" id="addressone" placeholder="From">
</section>
<section>
<input type="text" name="to" class="to address" id="addresstwo" placeholder="To">
</section>
<section>
<label>Time</label>
<input type="text" name="hrs" placeholder="Hours" class="t1 time">
<span>:</span>
<input type="text" name="mins" placeholder="Mins" class="t1 time">
<button class="button">Search</button>
</section>
</form>
</section>
更新
我得到這個輸出在我的控制檯
嘿關閉選民。爲什麼關了? – 2619