Jump to content

Possible to limit keyword entries to a certain length / kind of characters?


Recommended Posts

Hi,

 

Is it possible to limit the length of a keyword entry to a set number of characters? And if so how, please?

 

I have a workflow which, by way of a keyword entry, asks me to enter a date in DDMMYY format. I'd like it to limit me to inputting a maximum of 6 characters (numbers only, if possible) and / or to 'check' the input and ask me to re-input it if not in DDMMYY format.

 

Thanks in advance for any help received.

Link to comment
On 8/7/2023 at 9:17 PM, Stephen_C said:

You can use RegEx to validate the entry once the entry is given. There is an example in the Get Weekday of Date workflow on the Alfred Gallery (full disclosure: I wrote the workflow). However, I'm not aware of any method of restricting the input at the time it's requested (as opposed to validating it after it's given).

 

Stephen


Thanks. I know nothing about RegEx at the moment so I’ll take a look at your workflow and see if I can figure out how I could get it to work for me. 

 

On 8/7/2023 at 10:43 PM, vitor said:

To do real-time validation, use a Script Filter and set valid from the JSON to false when it doesn’t pass. I’d like to me more specific but that’s difficult without knowing more about the workflow.

 


Thanks. The workflow takes input from a combination of keyword entries and selections from list filters to form a URL used to perform a search on a website.
 

One component within the URL is the date, which has to be in DDMMYY format only. When I use the workflow I’m usually good at entering input in this format, but I’m interested in including a mechanism to fix things / ask me to re-enter things if entered with a typo / extra digit etc. And / or also to not accept dates prior to today’s date. 

 

I have no idea how I’d do this with a script filter, so any further advice would be appreciated. 

Link to comment

Add a Script Filter with default settings and this as the code:

 

# Check for wrong format
if ! /bin/date -jf '%d%m%y' "${1}" > /dev/null || [[ "${#1}" -ne 6 ]]
then
  printf '{ "items": [{ "title": "Wrong Format", "subtitle": "Date format be ddmmyy", "valid": false }] }'
  exit 1
fi

# Check for wrong date
readonly today="$(/bin/date '+%s')"
readonly new_date="$(/bin/date -jf '%d%m%y' "${1}" '+%s')"

if [[ "${today}" -gt "${new_date}" ]]
then
  printf '{ "items": [{ "title": "Wrong Period", "subtitle": "Date must not be prior to today", "valid": false }] }'
  exit 1
fi

# Everything correct
printf '{ "items": [{ "title": "Correct Date Format", "subtitle": "Press ↩ to continue", "arg": "%s" }] }' "${1}"

 

Link to comment
6 hours ago, vitor said:

Untested, but change both instances of %d%m%y to %H%M and "${#1}" -ne 6 to "${#1}" -ne 4.

 

Thank you - I've done that but now when I enter a 4 digit time (in HHMM format) I get the "Wrong period - Date must not be prior to today" error. That part of the script doesn't need to run in relation to a time, but it would be good if the script could check that that what is entered does amount to a time in HHMM format (ie - would give an error if I entered '2515' (hrs) for example)?

 

Many thanks for your help so far - hope this makes sense!

Link to comment
9 hours ago, vitor said:

Remove everything between # Check for wrong date and # Everything correct

 

Thank you - I've done that.

 

I've noticed a quirk; the script will 'approve' the following as valid HHMM entries: "231a" or "102g" (examples) - ie it will accept a letter as a valid last characted for some reason?

 

Thanks again!

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...