R
R
Rphoenix2020-06-21 16:26:18
Qt
Rphoenix, 2020-06-21 16:26:18

QML TextField date validation?

QML DATAEDIT Completed, but the only bug with months remained: it normally works out the number, and if I enter a month and it costs 06, it turns out 16, then it is reset. How to fix?

spoiler
import QtQuick 2.0
import QtQuick.Controls 1.4

Item {
id: dateEdit

readonly property date currentDate: calendar.selectedDate
readonly property bool isCalendarOpened: calendar.visible
property string color: "lightblue"

signal calendarOpened;
signal calendarClosed;

width: textField.width + btnOpen.width
height: textField.height

Column {
id: column
spacing: 2

Row {
spacing: 5
TextField {
id: textField
text: Qt.formatDate(calendar.selectedDate, "dd.MM.yyyy");
// readOnly: true

function validate_date(value)
{
var arrD = value.split(".");
arrD[1] -= 1;
var d = new Date(arrD[2], arrD[1], arrD[0]);
if ((d.getFullYear() == arrD[2]) && (d.getMonth() == arrD[1]) && (d.getDate() == arrD[0])) {
return true;
} else {

return false;
}
}

function getMonth(value)
{
var arrm = value.split(".");
arrm[1] -= 0;
return arrm[1];

}

function getDay(value)
{
var arrd = value.split(".");
arrd[0] -= 0;
return arrd[0];

}

Keys.onReleased:
{validate_date(text)?console.log("Current data: "+(text.substring(3,5))):{text=Qt.formatDate(calendar.selectedDate, "dd.MM.yyyy")}}
inputMask: "99.99.9999"
inputMethodHints: Qt.ImhDigitsOnly
Keys.onUpPressed: calendar.__selectNextDay()
Keys.onDownPressed: calendar.__selectPreviousDay()
}
Rectangle {
id: btnOpen
radius: 5
width: 30
height: textField.height
color: dateEdit.color

Text {
text: "*"
}

MouseArea {
anchors.fill: parent

onClicked: {
dateEdit.isCalendarOpened ? closeCalendar() : openCalendar()
}
}
}
}

Calendar {
id: calendar
visible: false
dayOfWeekFormat: Locale.ShortFormat
onClicked: {
closeCalendar()
}

function open()
{
visible = true
}

function close()
{
visible = false
}
}
}

function openCalendar()
{
calendar.open()
calendarOpened()
}

function closeCalendar()
{
calendar.close()
calendarClosed()
}
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question