Validate empty Date field

Hello Plumsail!

How can I validate an empty date field so that it has a value in it?

I have this code working but when I remove the values from the field it lets me proceed to the next wizard.

    //checks if last working day > leaving date
    fd.field('PSEmployee_Last_Working_Day').validators.push({
    name: '',
    error: "Please enter the Employee's last working day",
    validate: function(value) {
    if (fd.field('Question_2').value == 'Yes' && fd.field('Question_3').value == 'No'
    && fd.field('PSEmployee_Last_Working_Day').value == null){
    return false;
    }
    return true;       
    }
    });

Hello @DryChips,

This line is correct for validating the date field:

fd.field('PSEmployee_Last_Working_Day').value == null

Probably the issue is other part of the condition. You can try removing it and test.

Hi Margarita,

I have tested it by taking the other condition out and I have the same outcome. It still lets me proceed to the next wizard if I clear the field after picking a date.

@DryChips,

The validation works on my form. How do you clear the field? Programmatically or manually?

Perhaps the validation is simply not added to the field because of the syntax error. Please check the browser console for the errors when the form is loaded and when switching to the next wizard tab.

Hi,

I remove the value from the date field manually by highlighting it and hitting 'backspace' on the keyboard, next, I press the next button to proceed to the next wizard and it lets me proceed.

I found this unusual error in the console:

@DryChips,

There is a syntax error in your code.

You need to debug the code. Find the instructions in this post.

Hiya,

I have this regex but its not validating any of the values I enter in format: DD/MM/YYYY. I am running the validator on the date field.

I tested the regex and it works on single line text field. Is this a data type problem?

i tested the regex pattern ^\d{2}\/\d{2}\/\d{4}$ on this website and it has returned true:

Here is my code:

//Assignment Number Validation - [Wizard 2]
 fd.field('PSEmployee_Termination_Date').validators.push({
        name: '',
        error: "Please enter a Date in format DD/MM/YYYY",
        validate: function(value) {
            if ((/^\d{2}\/\d{2}\/\d{4}$/).test(value)) {
                    console.log(value);
            //Applies Colour to the field if Correct input
            $($(fd.field('PSEmployee_Termination_Date').$el).find('input')[0]).attr('style', 'border-color: #6CEE5A;');
            return true;
            }
            return false;
        }
 });

What am I doing wrong?

@DryChips,

If the user enters the date in an invalid format, the value of the date field is null. So it can't be checked against the RegEx pattern. You also need to update the line that changes field border color.
You can try out this code:

//Assignment Number Validation - [Wizard 2]
 fd.field('PSEmployee_Termination_Date').validators.push({
        name: '',
        error: "Please enter a Date in format DD/MM/YYYY",
        validate: function(value) {
            if (value) {
                    console.log(value);
            //Applies Colour to the field if Correct input
            $($(fd.field('PSEmployee_Termination_Date').$el).find('span.k-picker-wrap.k-state-default')[0]).attr('style', 'border-color: #6CEE5A;');
            return true;
            }
            return false;
        }
 });
1 Like