You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to format input into a text field in a plugin
The text field is supposed to only allow input in the format "X 00000 00000"
on windows 11 (Qfield 3.4.5) this works correctly and restricts the user to the specifed format.
on android (honor magic 4 pro) (Qfield 3.4.7) it does not.
I've attached the plugin - when selecting 'Irish Grid' as the input method the text field is supposed to be restricted to the format "X 00000 00000"
Has anyone any ideas what I've done wrong?
Thanks in advance,
Tony.
//// The validation code is:
TextField {
id: irishGridInputBox
Layout.fillWidth: true
placeholderText: "Enter in format: X 00000 00000"
// Custom validation logic
onTextChanged: {
// Remove any non-alphanumeric characters (except spaces)
var cleanedText = irishGridInputBox.text.replace(/[^A-Za-z0-9\s]/g, '')
// Ensure the first character is a valid letter from the matrix
if (cleanedText.length > 0 && !igletterMatrix[cleanedText[0].toUpperCase()]) {
cleanedText = cleanedText.substring(1)
}
// Insert spaces at the correct positions
if (cleanedText.length > 1 && cleanedText[1] !== ' ') {
cleanedText = cleanedText[0] + ' ' + cleanedText.substring(1)
}
if (cleanedText.length > 7 && cleanedText[7] !== ' ') {
cleanedText = cleanedText.substring(0, 7) + ' ' + cleanedText.substring(7)
}
// Ensure the characters after the first space are digits
if (cleanedText.length > 2) {
var firstNumberPart = cleanedText.substring(2, 7)
if (!/^\d{0,5}$/.test(firstNumberPart)) {
firstNumberPart = firstNumberPart.replace(/\D/g, '')
cleanedText = cleanedText.substring(0, 2) + firstNumberPart + cleanedText.substring(7)
}
}
// Ensure the characters after the second space are digits
if (cleanedText.length > 8) {
var secondNumberPart = cleanedText.substring(8, 13)
if (!/^\d{0,5}$/.test(secondNumberPart)) {
secondNumberPart = secondNumberPart.replace(/\D/g, '')
cleanedText = cleanedText.substring(0, 8) + secondNumberPart + cleanedText.substring(13)
}
}
// Limit the total length to 13 characters (X 00000 00000)
if (cleanedText.length > 13) {
cleanedText = cleanedText.substring(0, 13)
}
// Update the text field
irishGridInputBox.text = cleanedText
}
// Function to validate the final input
function isValidInput() {
var regex = /^[A-Za-z]\s\d{5}\s\d{5}$/
return regex.test(irishGridInputBox.text) && igletterMatrix[irishGridInputBox.text[0].toUpperCase()]
}
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi,
I'm trying to format input into a text field in a plugin
The text field is supposed to only allow input in the format "X 00000 00000"
I've attached the plugin - when selecting 'Irish Grid' as the input method the text field is supposed to be restricted to the format "X 00000 00000"
Has anyone any ideas what I've done wrong?
Thanks in advance,
Tony.
qfield-plugin-addpoints.zip
Beta Was this translation helpful? Give feedback.
All reactions