Applies To: Information 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:
- DateTimeField1 - date field;
- TextField and dStatus - text fields;
- SubmitButton1 - for form submission.
Step 2. Navigate to Options → Rules → Client Rules → Submit (system event)
Place cursor before the Submit form to SharePoint action.
Step 3. In action adding dropdown select Action
Step 3.1 In Actions list select Execute script
Step 3.2 Press Click to enter script button in order to enter script
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
Step 5. Place cursor under the Execute script action and select If ... Then ... condition builder
Step 5.1 Select If ... Then ... block and click Else block in the rules ribbon
Step 5.2 Result
Step 6. Place cursor into If part → select PDF Field Value
Step 6.1 Select a field that will be checked in the condition. dStatus in this example
Step 6.2 Select condition that will be checked. Equals in this example
Step 6.3 Select the value dStatus field will be compared to. Manual string value... in this example
Step 6.4 Result
Step 7. Place cursor into Then part and select Action... builder
Step 7.1 in Actions list select Submit form to SharePoint action
Step 8. Place cursor into Else block and select Action... builder
Step 8.1 Select Execute script action in the Actions list
Step 8.2 Result
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 → Properties → Display: Hidden
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
Add date and click Submit - warning appears
Step 11.2 Click OK and add correct date - submit will pass validation.
0 Comments