Follow

Custom Date Field validation on form submit

Applies ToInformation Worker for SharePoint 2013/2016/2019 and Cloud

This article describes how to add validation message for date fields before form is submitted. 

 

Step 1. Create template with the following controls:

1.png

  • DateTimeField1 - date field;
  • TextField and dStatus - text fields;
  • SubmitButton1 - for form submission.

Step 2. Navigate to Options → Rules → Client Rules → Submit (system event)

3.png

Place cursor before the Submit form to SharePoint action.

Step 3. In action adding dropdown select Action

4.png

Step 3.1 In Actions list select Execute script

extra.png

Step 3.2 Press Click to enter script button in order to enter script

5.png

Add the following script and click Save:

this.getField("dStatus").value="";
var result = "";
var dValue = "";
var val = ["TextField1", "DateTimeField1"]; //list all fields that must be checked

//runs through all fields and checks date format
for(var i = 0; i<val.length; i++){
dValue = this.getField(val[i]).value;
if(dValue){
result = dValue.match(/^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[012])\/(19|20)\d\d$/); //checks if listed fields have dd/mm/yyyy date format
}

//fields that don't match the format are added to the list that is stored in dStatus field
if (result == null){
if(this.getField("dStatus").value==""){
this.getField("dStatus").value = val[i];
}
else this.getField("dStatus").value = this.getField("dStatus").value +", "+val[i];
}
}

Note: validation format pattern can be changed by replacing 

result = dValue.match(/^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[012])\/(19|20)\d\d$/);

With:

for mm/dd/yyyy use result = dValue.match(/^(0[1-9]|1[012])\/(0[1-9]|[12][0-9]|3[01])\/(19|20)\d\d$/);

 

 

Step 4. Delete Submit form to SharePoint action. Select Submit form to SharePoint action → click Tools icon → choose Delete selected

6.png

Step 5. Place cursor under the Execute script action and select If ... Then ... condition builder

7.png

Step 5.1 Select If ... Then ... block and click Else block in the rules ribbon

8.png

Step 5.2 Result

9.png

Step 6. Place cursor into If part → select PDF Field Value

10.png

Step 6.1 Select a field that will be checked in the condition. dStatus in this example

11.png

Step 6.2 Select condition that will be checked. Equals in this example

12.png

Step 6.3 Select the value dStatus field will be compared to. Manual string value... in this example

13.png

Step 6.4 Result 

14.png

Step 7. Place cursor into Then part and select Action... builder

a.png

Step 7.1 in Actions list select Submit form to SharePoint action

a1.png

Step 8. Place cursor into Else block and select Action... builder

a2.png

Step 8.1 Select Execute script action in the Actions list

a3.png

Step 8.2 Result

a4.png

Step 9. Click on Click to modify script in Else block and add the following code:

app.alert({ cMsg: "The value entered does not match the format of the field [" +this.getField("dStatus").value+"].Format should be dd/mm/yyyy ", cTitle: "" }); 

If dStatus field is not empty, then user receives warning that listed fields have incorrect date format

Step 10. Make dStatus field hidden because it is used as a global variable. Navigate back to Design tab → select dStatus field → PropertiesDisplay: Hidden

2.png

Step 11. Publish the template and open a new form instance

Step 11.1 Add incorrect format into Text Field1, since we need dd/mm/yyyy date format, we will use 12/20/2017 in order to check validation - there is no 20th month

extra2.png

Add date and click Submit - warning appears

Step 11.2 Click OK and add correct date - submit will pass validation.

Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.
Powered by Zendesk