mIRC Home    About    Download    Register    News    Help

Print Thread
#153967 25/07/06 02:16 AM
Joined: Aug 2004
Posts: 7,252
R
RusselB Offline OP
Hoopy frood
OP Offline
Hoopy frood
R
Joined: Aug 2004
Posts: 7,252
In a dialog I have two edit boxes, one containing the format for the date display, the other for the time display.
The default settings are ddd mmm dd yyyy & hh:nn:ss tt

I was looking for a way to prevent someone from changing these to an improper date/time display format.
Example for the date: dnd mhm dd yssy

I asked around and did some reading, coming up with the following for a regex, but I guess my knowledge is still lacking at some point, as it doesn't work completely.

If someone could fix this up and explain what and why they did, I (hopefully) will be able to modify the code so that it works similarily for the box with the time format.

Here's what I was able to come up with.
Code:
 on *:dialog:channel_control:edit:103,105:{
  var %date.fmt = $did(Channel_Control,103)
  var %time.fmt = $did(Channel_Control,105)
  if !$regex(%date.fmt,[^dmy.-\s]) {
    if $regex(%date.fmt,|d|dd|ddd|dddd) && !$regex(%date.fmt,\bddddd\b) {
      if $regex(%date.fmt,|m|mm|mmm|mmmm) && !$regex(%date.fmt,\bmmmmm\b) {
        if $regex(%date.fmt,|y|yy|yyy|yyyy) && !$regex(%date.fmt,\byyyyy\b) {
        }
        else {
          echo -a error in year
          did -ra $dname $did $iif($did == 105,hh:nn:ss tt,ddd mmm dd yyyy)
        }
      }
      else {
        echo -a error in month
        did -ra $dname $did $iif($did == 105,hh:nn:ss tt,ddd mmm dd yyyy)
      }
    }
    else {
      echo -a error in day
      did -ra $dname $did $iif($did == 105,hh:nn:ss tt,ddd mmm dd yyyy)
    }
  }
  .timertime -m 0 500 title
}
 

Dialog ID 103 contains the date format, and 105 the time format.

#153968 25/07/06 03:13 AM
Joined: Oct 2005
Posts: 1,741
G
Hoopy frood
Offline
Hoopy frood
G
Joined: Oct 2005
Posts: 1,741
These two regexes should verify the time and date codes:

Code:
alias df.check {
  var %date.fmt = $1-
  if ($regex(date,%date.fmt,/(^\s*$|d{5,}|m{5,}|y{5,}|(?<!y)y{3}(?!y)|[^dmy -/\\])/)) echo 4 -a Invalid format: $regml(date,1)
  else echo 3 -a Valid format
}

alias tf.check {
  var %time.fmt = $1-
  if ($regex(time,%time.fmt,/(^\s*$|h{3,}|H{3,}|n{3,}|o{3,}|s{3,}|t{3,}|T{3,}|(?<!o)o(?!o)|z{4,}|[^hHnostTz :-])/)) echo 4 -a Invalid format: $regml(time,1)
  else echo 3 -a Valid format
}


All time/date codes are CASE SENSITIVE (ie, DD is not valid, while dd is valid).

For the date checker, the following codes are valid:
d
dd
ddd
dddd
m
mm
mmm
mmmm
yy
yyyy

and the following separators are valid:
(space)
-
/
\


For the time checker, the following codes are valid:
h
hh
H
HH
n
nn
s
ss
t
tt
T
TT
oo
z
zz
zzz

and the following separators are valid:
(space)
-
:

The regexes are negative-matching. That is, if the regex is matched, it means the format is INVALID. It also considers zero-length strings (null) and strings that only consist of space (space, tab, etc) characters to be invalid.

Edit: One thing I noticed about your original code, is that it reverts to the default format if the entered format is invalid. That reminds me of the annoying way windows reverts to the original name when you rename a file to one that already exists.. making you retype the whole thing again.

-genius_at_work

Last edited by genius_at_work; 25/07/06 03:30 AM.
#153969 25/07/06 03:56 AM
Joined: Aug 2004
Posts: 7,252
R
RusselB Offline OP
Hoopy frood
OP Offline
Hoopy frood
R
Joined: Aug 2004
Posts: 7,252
Thanks for your help.
I did notice, when testing your code, The hour display permits the following formats that should be invalid: Hhh, HHhh, hHh, hHHh, hhH, hhHH.

I've managed to alter the code so that the oo comes up valid in the date format (which is something I didn't think about, but do like), but I was wondering, is it possible to have it accept the single o as a oo, and force it to only be allowed after d or dd?

That's all that I can think of at the moment, hope it's not too much.

Regarding the reset that you mentioned, I think I'm going to put in a timer so that it resets to the default if the format is invalid for 5 seconds (that should be more than long enough for a person to change the display from an invalid to a valid format)

#153970 25/07/06 04:38 AM
Joined: Oct 2005
Posts: 1,741
G
Hoopy frood
Offline
Hoopy frood
G
Joined: Oct 2005
Posts: 1,741
Here is a modified code:

Code:
alias df.check {
  var %date.fmt = $1-
  if ($regex(date,%date.fmt,/(^\s*$|d{5,}|m{5,}|y{5,}|(?<!y)y{3}(?!y)|o{3,}|(?<!o)o(?!o)|(?:[^d]|d{3,}|^)oo|[^dmoy -/\\])/)) echo 4 -a Invalid format: $regml(date,1)
  else echo 3 -a Valid format
}

alias tf.check {
  var %time.fmt = $1-
  if ($regex(time,%time.fmt,/(^\s*$|[hH]{3,}|hH|Hh|n{3,}|s{3,}|[tT]{3,}|tT|Tt|[^z{4,}|[^hHnstTz :-])/)) echo 4 -a Invalid format: $regml(time,1)
  else echo 3 -a Valid format
}


Changes:

Date:
- 1 or 3+ 'o' is invalid
- 'oo' not preceeded by 1-2 'd' is invalid

Time:
- any string of 3+ 'h' or 'H' is invalid
- 'hH' is invalid
- 'Hh' is invalid
- any string of 3+ 't' or 'T' is invalid
- 'tT' is invalid
- 'Tt' is invalid
- any 'o' is invalid

I'm not sure what you mean by accepting a single 'o' as that is not a valid code. If you meant that you want to replace a 'o' with 'oo' automatically, that can be done with a $regsub(ex). Example:
Code:
alias replace.o {
  var %to.rep = $1-
  %to.rep = $regsubex(,%to.rep,/(?<!o)o(?!o)/,oo)
  echo 2 -a %to.rep
}


-genius_at_work

Last edited by genius_at_work; 25/07/06 04:43 AM.
#153971 25/07/06 05:21 AM
Joined: Aug 2004
Posts: 7,252
R
RusselB Offline OP
Hoopy frood
OP Offline
Hoopy frood
R
Joined: Aug 2004
Posts: 7,252
You understood what I meant perfectly. Thanks for your help.
I've added your name to the credits list for this project.

#153972 25/07/06 01:52 PM
Joined: Oct 2005
Posts: 1,741
G
Hoopy frood
Offline
Hoopy frood
G
Joined: Oct 2005
Posts: 1,741
No problem. wink

-genius_at_work

#153973 26/07/06 08:09 AM
Joined: Aug 2004
Posts: 7,252
R
RusselB Offline OP
Hoopy frood
OP Offline
Hoopy frood
R
Joined: Aug 2004
Posts: 7,252
Slight problem encountered. When testing I tried a date format of dddd mdmm dd yyyy and the regex didn't catch it as an invalid format, somewhat understandably, however, I can't think of anyone that's going to want a date being displayed as "Wednesday 72607 26 2006". I think you can see the problem, however, a solution is beyond my understanding of regex (heck the code you wrote is a bit beyond my understanding of regex, although I have to give myself credit for being able to (somewhat) understand what your regex is doing).

RusselB looks back at the previous paragraph and sighs thinking he'd rather have cash than credit

#153974 26/07/06 02:07 PM
Joined: Oct 2005
Posts: 1,741
G
Hoopy frood
Offline
Hoopy frood
G
Joined: Oct 2005
Posts: 1,741
The problem is that your example format is completely valid.. it's just illogical. I don't know how important a valid date/time format is to your script, but there are just too many variations to make a regex that will stop all illogical formats. If you won't/can't give the operator a bit of responsibility in choosing a logical format, then perhaps you should use a drop-down box with 10-20 preset formats for date and time. That way the user can't fux things up.

-genius_at_work

#153975 27/07/06 12:01 AM
Joined: Aug 2004
Posts: 7,252
R
RusselB Offline OP
Hoopy frood
OP Offline
Hoopy frood
R
Joined: Aug 2004
Posts: 7,252
I was afraid of that. I'll give it some more thought and see what I can come up with.

#153976 27/07/06 12:14 AM
Joined: Sep 2003
Posts: 4,230
D
Hoopy frood
Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
u should just make sure it works, if they want there date in some stupid format thats there bussiness

That date format well Its dumb, but its not wrong!

PS: I always put mine into YYYYMMDD becuase I cant stand looking at dates and going 11/12/2006 is that 11th of the 12th month or 12th of the 11th month?!?!?!?!?!?
I beleive YYYYMMDD is Japan style (not sure on this) but it suits me real fine as well, becuase you can sort on it as well since its most significant value to the least.

#153977 27/07/06 02:04 AM
Joined: Aug 2004
Posts: 7,252
R
RusselB Offline OP
Hoopy frood
OP Offline
Hoopy frood
R
Joined: Aug 2004
Posts: 7,252
Agreed, and genius' codes work fine for that, and if someone does try to put in a really weird format (eg: dnd msm dd yy) there is a change in the display showing the "error" codes (in this case the n & s), and a 5 second timer is started, which resets to the default value if the error(s) haven't been corrected.


Link Copied to Clipboard