cancel
Showing results for 
Search instead for 
Did you mean: 

Date validation

0 Kudos

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.

saurabh_vakil
Active Contributor
0 Kudos

Have you tried setting the displayFormat for your DatePicker as displayFormat="dd/MM/yyyy"

0 Kudos

Hai Saurabh, I tried that. We are allowed to create the record with above wrong date values.

boghyon
Product and Topic Expert
Product and Topic Expert

Accepted Solutions (1)

Accepted Solutions (1)

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'
  }
}"
boghyon
Product and Topic Expert
Product and Topic Expert
0 Kudos

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.

Answers (1)

Answers (1)

0 Kudos

Hi Pavankumar A N,

Use the given Properties in Date Picker

valueFormat and displayFormat

< DatePicker valueFormat="dd.MM.yyyy" >

BR

Arun Mathew

0 Kudos

Thank you Arun for the response, we tried all these but we can create a record with 124341/123312/123431 this value also. Can you please help me resolve this?

0 Kudos

In date picker, you can select the date from the calendar instead of enter the value.

0 Kudos

go through the following link it may help you out!..

Date Picker

BR,

Arun Mathew.