String Validity Checking Problem

Discussion in 'AutoCAD' started by David Kozina, Jul 9, 2004.

  1. David Kozina

    David Kozina Guest

    Perhaps I'm not seeing the trees for the forest, but is there a simple way
    to check if the user has provided valid input data?

    In this case, I need to check if the user has input a valid string of
    'project phases'.
    The *order* of the string characters does not matter, what *does* matter is
    that *only* the following characters are valid: "" D E F 1 2 3 4 5 6 7 8 9 0

    IOW:
    "" would be perfectly fine
    "FE" would be perfectly fine
    "4E312" would be perfectly fine
    "235G1" would be partially valid (G invalid)
    "WACBP" would NOT be valid

    What follows is what I've come up with thus far - it seems straightforward
    enough to me, and I *think* it should work - but I can't help but think it
    borders on "klunky", which I why I'm asking here. If there's a faster or
    more elegant way, I'd sure like to hear about it:

    ; Set to Upper Case and convert to char list
    (setq Phase_lst
    (vl-string->list (strcase PhaseInput_str))
    );_end setq
    (setq ValidChar_lst
    ; 0 1 2 3 4 5 6 7 8 9 D E F
    '(48 49 50 51 52 53 54 55 56 57 68 69 70)
    );_end setq
    ; remove invalid chars from list
    ; did user input anything other than ""?
    (if Phase_lst
    ; then
    (setq Phase_lst
    (vl-remove-if-not
    '(lambda (ele)
    (member ele ValidChar_lst)
    )
    Phase_lst
    )
    )
    ; else
    ;
    );_end if

    Any suggestions?

    Appreciatively,
    David Kozina
     
    David Kozina, Jul 9, 2004
    #1
  2. David Kozina

    David Bethel Guest

    You could step through the string;

    (setq ts (getstring "\nTest String: "))
    (setq i 1)
    (repeat (strlen ts)
    (if (wcmatch (substr ts i 1) "[ D E F 1 2 3 4 5 6 7 8 9 0 ]")
    (princ "\nTrue")
    (princ "\nFalse"))
    (setq i (1+ i)))

    -David
     
    David Bethel, Jul 9, 2004
    #2
  3. David Kozina

    Jürg Menzi Guest

    Hi David

    How about this easy solution:

    (not (wcmatch YourString "*[~DEF0-9]*"))

    Cheers
     
    Jürg Menzi, Jul 10, 2004
    #3
  4. David Kozina

    Jürg Menzi Guest

    Into a function it would look like this:

    (defun GetProjPhase ( / ExLoop TmpStr)
    (while (not ExLoop)
    (setq TmpStr (strcase (getstring "\nProject Phase: ")))
    (if (or
    (eq TmpStr "")
    (wcmatch TmpStr "*[~DEF0-9]*")
    )
    (princ "Project Phase: invalid name. ")
    (setq ExLoop T)
    )
    )
    TmpStr
    )

    Cheers
     
    Jürg Menzi, Jul 10, 2004
    #4
  5. David Kozina

    David Kozina Guest

    Jürg,

    Thanks! I think that is probably the 'tree' I've been looking for.
    Very neat and tidy (which I suspected would be the case),
    although not as "easy" for me to see as it was for you.

    The other day I purchased the O'Reilly Press book "Mastering Regular
    Expressions" -
    Perhaps that'll help me in the future with problems like this.
    http://www.oreilly.com/catalog/regex2/

    One thing I was trying to accomplish with the code I posted was the ability
    to weed out the invalid characters (like G or C) (as well as duplicates -
    which I realized and added later). IOW, if the user entered
    "53C27D1432BE62" (Brainless user, I know, but hey...), the String Checker
    routine WOULD accept it as *sufficiently* valid, and then eliminate any
    incorrect (or duplicate) characters: B C 3 2, leaving just "5327D14E6" in
    this case, which would be acceptable. If *everything* in the string was
    incorrect, like say, "GASP!", you would still end up with "", which *would
    be* acceptable. Does this make sense to anyone?

    Perhaps, though, in this case I should just force the user to "shape up" and
    enter a correct string via a while..loop, using your solution, which would
    just reject any string containing an incorrect character .

    It seems to be kind of a gray area - At what point is it useful (programming
    design-wise) to allow some margin of error from the user, and still run, or
    instead just contain a brutally strict interface. There's got to be a kind
    of 'sweet-spot' somewhere between the two extremes, no?

    Anyone have any thoughts on this issue?

    (Thanks again, Jürg!)

    Best regards,
    David Kozina
     
    David Kozina, Jul 10, 2004
    #5
  6. David Kozina

    Jürg Menzi Guest

    Hi David

    You're always welcome...:cool:
    Also reading the help file can be sometimes very helpful...
    In this case the user gets an unwanted name - I would prefer an error message
    like 'Stupid - allowed characters are 0-9 or D,E,F' or something like this...
    hehehe ;-)

    Cheers
     
    Jürg Menzi, Jul 10, 2004
    #6
  7. David Kozina

    Jürg Menzi Guest

    Ahem, shoud be:
    'Bonehead - allowed characters are 0-9 and D,E,F'

    My english... grumble

    Cheers
     
    Jürg Menzi, Jul 10, 2004
    #7
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.