cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Date validation

Former Member
0 Likes
2,435

How can I restrict user to enter date in only dd/mm/yyyy format? I tried regular expression in datepicker but it is too complex for others if they want to make modifications. Please help me resolve this. Thanks in advance.

View Entire Topic
boghyon
Product and Topic Expert
Product and Topic Expert

This answer suggests to use binding type instead (such as 'sap.ui.model.odata.type.DateTime') which restricts the input value to be stored in the model if it could not be parsed or validated.

Example:

<DatePicker placeholder="DD/MM/YYYY" value="{
  path: 'myODataModel>myDate',
  type: 'sap.ui.model.odata.type.DateTime',
  formatOptions: {
    pattern: 'dd/MM/yyyy'
  },
  constraints: {
    isDateOnly: true,
    displayFormat: 'Date'
  }
}"

The value will be always displayed in the dd/MM/yyyy pattern. But the model stores the value in JS-Date object (Internally, the thirdparty lib datajs will convert it again to OData conform format before it's sent).

Finally, register the control in the message manager to activate automatic message generation as shown in the documentation topic UI Messages.

Sample JSBin: https://jsbin.com/seyutiw/edit?js,output

_____________

If the model is not an ODataModel but just a plain JSONModel:

<DatePicker placeholder="DD/MM/YYYY" value="{
  path: 'myJSONModel>myDate',
  type: 'sap.ui.model.type.Date',
  formatOptions: {
    pattern: 'dd/MM/yyyy'
  }
}"
Former Member
boghyon
Product and Topic Expert
Product and Topic Expert
0 Likes

pavankumaran#overview

The error message you were getting was due to the missing comma which I fixed in the above answer. I also added another snippet in case the "Project" model is a JSONModel instead of OData. You can check the JSBin sample I added to the answer.

Please try again and let us know if this solution was helpful.