Having problem to disable validation in idealforms, I would like to have following fields not being validated
postcode3, memo2, previous_year, previous_month
until value in current_year is less then 3
$(document).ready(function(){
$('#current_year').on("input", function() {
var dInput = this.value;
console.log(dInput);
$('#current_year').text(dInput);
$('form.idealforms').idealforms('addRules', {
'postcode3': ( parseInt( $('#current_year').val() ) < 3) ? 'required' : '',
});
});
$('form.idealforms').idealforms({
silentLoad: false,
rules: {
'postcode2': 'required',
'memo1': 'required',
'current_year': 'required',
'current_month': 'required',
},
onSubmit: function(invalid, e) {
e.preventDefault();
$('#invalid')
.show()
.toggleClass('valid', ! invalid)
.text(invalid ? (invalid +' invalid fields') : 'All good!');
}
});
Assuming the rest of your code is correct I think the issue is that val()
returns a string
and not a number
. You'll need to write your expressions like so
( parseInt( $('#current_year').val() ) < 3 ) : 'required' : '';
or compare val() to the string value of the number.
( $('#current_year').val() < '3' ) : 'required' : '';
Edit
There is a second issue in that once your rules are declared, nothing in your code changes the rules. So if onload $('#current_year').val()
is less then 3, your fields will always be required.
According to the plugin docs you can add rules after initializing the plugin like so
$('form').idealforms('addRules', {
'comments': 'required minmax:50:200'
});
So you might be able to do use this to update the rules like so.
$('#current_year').on("input", function() {
$('form.idealforms').idealforms('addRules', {
'postcode3': ( parseInt( $('#current_year').val() ) < 3) ? 'required' : '',
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.