I have 4 input fields as below screen.
Code is below.
<input class="form-control licence-group" name="LicenceNumber1" id="LicenceNumber1" type="text">
<input class="form-control licence-group" name="LicenceNumber1Date" id="LicenceNumber1Date" type="text">
<input class="form-control licence-group2" name="LicenceNumber2" id="LicenceNumber2" type="text">
<input class="form-control licence-group2" name="LicenceNumber2Date" id="LicenceNumber2Date" type="text">
I'm using this with jQuery Step Plugin.
I want to validate this with following conditions.
- All fields are required
- If filled LicenceNumber1 and LicenceNumber1Date then no need LicenceNumber2 and LicenceNumber2Date fields.
- Same as if filled LicenceNumber2 and LicenceNumber2Date then no need LicenceNumber1 and LicenceNumber1Date fields.
- Every pair of input fields are required(EG: Licence number and Date)
- If filled 3 fields other remaining field is required. EG: licence 1, licence1 date, licence 2 filled then licence2 date is required.
I used the below code. But it doesn't work.
rules: {
LicenceNumber1: {
required: function (element) {
return (($("#LicenceNumber1Date").is(':empty')) && ($("#LicenceNumber2").is(':empty') || $("#LicenceNumber2Date").is(':empty')) );
}
},
LicenceNumber1Date: {
required: function (element) {
return (($("#LicenceNumber1").is(':empty')) && ($("#LicenceNumber2").is(':empty') || $("#LicenceNumber2Date").is(':empty')));
}
},
LicenceNumber2: {
required: function (element) {
return (($("#LicenceNumber2Date").is(':empty')) && ($("#LicenceNumber1").is(':empty') || $("#LicenceNumber1Date").is(':empty')));
}
},
LicenceNumber2Date: {
required: function (element) {
return (($("#LicenceNumber2").is(':empty')) && ($("#LicenceNumber1").is(':empty') || $("#LicenceNumber1Date").is(':empty')));
}
}
}
Please assume I have changed the input IDs to understandable. If it's possible without jquery validation plugin and using normal jquery it's also fine. If someone can help me to correctly validate this really appreciate your help. Thank you