I need to validate the “NOT REQUIRED DATE FIELD” for the correct input format. If the user enters letters or a date in the date field in the wrong format, the user should get an error asking them to use a selection from the calendar. But also the user should be able to leave the field blank. I have tried the following options but it doesn't work for an empty field, I can't leave the field blank.
There is no problem with checking the input format, there is a problem with leaving the field blank.
Example 1
fd.field('CustomerContractDate').validators.push({
name: 'MyCustomValidator',
error: '!!!!!!!!',
validate: function(value) {
if (fd.field('CustomerContractDate').value == null && fd.validators.length == 0) {
this.error = 'Use the selection or enter manually (dd.mm.yyyy)';
return false;
}
return true;
}
});
Example 2
fd.field('WarrantyStart').addValidator({
name: 'WarrantyStart validator',
error: 'Warranty From is in incorrect format. Please, use DatePicker',
validate: function(value) {
return !isNaN(Date.parse(value));
}
});
Example 3
fd.field('DatePickerField').addValidator({
name: 'DatePickerValidator',
error: "Please use the date picker to select a date.",
validate: function(value) {
if (!value) {
return false;
}
return true;
}
});
Example 4
// Set the date format to 'dd.MM.yyyy'
datePickerField.dateFormat = 'dd.MM.yyyy';
fd.field('CustomerContractDate').validators.push({
name: 'DatePickerValidator',
error: "Please use the date picker to select a date.",
validate: function() {
// Check if the value was manually entered (not selected from the date picker)
if (CustomerContractDate.value && !CustomerContractDate.control().isDateSelected())
{
return true;
}
return false;
}
I'm afraid this isn't possible at the moment, since the field is considered filled in only if the input is correctly formatted. I suggest making the date field required and adding a "No date" toggle to the form that would disable the date field and make it not required.
Thanks for the reply. Then what are the options to check fields for incorrect data entry? As far as I understand this behavior is caused by the editor, in the original Share Point the field can be checked.
Is there any possibility to restrict manual data entry so that the user only uses date picker?
And most importantly, not making the field mandatory.
Please explain to me why the date field works so strangely. Even if I use this code but I don't change anything in the field, just when I open the form, I still see “test”. as if the field is changed when I open the form and when I save the form even if I haven't touched the field.
The field value is only updated when a correct date is entered, either via a date picker or the textbox. Any JavaScript code that changes the value will trigger the 'change' event as well.
You can access the text in the textbox like so:
fd.field('Date').$el.querySelector('input').value
If the fd.field('Date').value is empty while the fd.field('Date').$el.querySelector('input').value is not, that means the date was entered incorrectly.
Here's the least suitable solution I've come up with. But it's not the best. So very much waiting for a normal solution, at least leave it the same as this standard Share Point online form.
Set the default value of the date field for example 01.01.1999. Then add the following code.
fd.field('CustomerContractDate').addValidator({
name: 'CustomerContractDate validator',
//error: 'Enter the correct date.',
validate: function(value) {
if (fd.field('CustomerContractDate').value == null || fd.validators.length == 0)
{
alert('Enter the correct Contract Date or leave the current value');
fd.field('CustomerContractDate').value = new Date('01.01.1999');
}
return !isNaN(Date.parse(value));
}
});