Author Topic: How to force user input 5 digits?  (Read 3178 times)

0 Members and 1 Guest are viewing this topic.

MeasureUp

  • Bull Frog
  • Posts: 462
How to force user input 5 digits?
« on: February 12, 2013, 04:54:33 PM »
This is a sample:
Code: [Select]
(getstring "Enter \"B\" followed by 5 digits no more or less: ")
Thanks for your help.

BlackBox

  • King Gator
  • Posts: 3770
Re: How to force user input 5 digits?
« Reply #1 on: February 12, 2013, 05:28:41 PM »
Code - Auto/Visual Lisp: [Select]
  1.   (and
  2.     (setq s (strcase
  3.               (getstring
  4.                 "Enter \"B\" followed by 5 digits no more or less: "
  5.               )
  6.             )
  7.     )
  8.     (= "B" (substr s 1 1))
  9.     (= 6 (strlen s))
  10.   )
  11.   ;; <-- do something
  12. )
  13.  




You should really start reading the developer documentation.
"How we think determines what we do, and what we do determines what we get."

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: How to force user input 5 digits?
« Reply #2 on: February 12, 2013, 06:31:24 PM »
Code - Auto/Visual Lisp: [Select]
  1. ;;  CAB 12.26.2009
  2. ;;  example Get number within a Numeric Range
  3. ;;    Arguments
  4. ;;  If nil, Ignore MinNum or MaxNum or DefNum
  5. ;;  msg and ErrMsg may be nil as well
  6. (defun GetNumInRange(MinNum MaxNum DefNum msg ErrMsg / num)
  7.   (or msg (setq msg "\nEnter number: "))
  8.   (or ErrMsg (setq ErrMsg "\nEntry is out of range. Try Again."))
  9.   (while
  10.     (cond
  11.       ((null (setq num (getdist msg)))
  12.        (if DefNum
  13.          (null (setq num DefNum))
  14.          (princ ErrMsg)
  15.        )
  16.       )
  17.       ((and MinNum (< num MinNum))
  18.        (princ ErrMsg))
  19.       ((and MaxNum (> num MaxNum))
  20.        (princ ErrMsg))
  21.     )
  22.   )
  23.   num
  24. )

Code: [Select]
(defun c:test()
  (print
  (GetNumInRange 10 50 39 "\nEnter number [10 =< Num >= 50]."
                     "\nError - entry is out of range. Try Again.")
    )
  (princ)
 
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: How to force user input 5 digits?
« Reply #3 on: February 12, 2013, 07:36:05 PM »
Maybe:

Code: [Select]
(defun c:test ( / str )
    (while
        (not
            (or
                (= "" (setq str (strcase (getstring "\nEnter \"B\" followed by 5 digits: "))))
                (wcmatch str "B#####")
            )
        )
        (princ "\nPlease enter a string of the form \"B#####\".")
    )
    (if (/= "" str)
        (princ "\nWe have a winner!")
    )
    (princ)
)

BlackBox

  • King Gator
  • Posts: 3770
Re: How to force user input 5 digits?
« Reply #4 on: February 12, 2013, 08:40:47 PM »
Code: [Select]
(wcmatch str "B#####")

... Brilliant.
"How we think determines what we do, and what we do determines what we get."

Stefan

  • Bull Frog
  • Posts: 319
  • The most I miss IRL is the Undo button
Re: How to force user input 5 digits?
« Reply #5 on: February 13, 2013, 02:40:35 AM »
Maybe this
Code - Auto/Visual Lisp: [Select]
  1. (defun get_n_digits (n / s msg gr c i)
  2.   (grread t)
  3.   (princ (setq i 0 s "" msg (strcat "\rEnter " (itoa n) " digits number: ")))
  4.   (while
  5.     (and
  6.       (< i n)
  7.       (= (car (setq gr (grread nil 2))) 2)
  8.       )
  9.       (if
  10.          (< 47 (setq c (cadr gr)) 58)
  11.          (setq i (1+ i)
  12.                s (strcat s (chr c))
  13.                )
  14.         (if
  15.           (= c 8)
  16.           (setq i (max (1- i) 0)
  17.                 s (substr s 1 i)
  18.                 )
  19.           (princ (strcat "\nInvalid character \"" (chr c) "\"\n"))
  20.          )
  21.         )
  22.     (princ (strcat msg s))
  23.     )
  24.   (print)
  25.   (if (= i n) s (not (princ (strcat "\nPlease enter exactly " (itoa n) " digits\n"))))
  26. )

Code: [Select]
(setq nr (get_n_digits 5))
Description:
-only valid digits are counted 0, 1, ..., 9
-you can press backspace to delete last valid character
-no need to press enter at the end. The routine ends once the n-th number is entered
-returns a string

pBe

  • Bull Frog
  • Posts: 402
Re: How to force user input 5 digits?
« Reply #6 on: February 13, 2013, 06:09:51 AM »
Autocad version in alternative reality (parallel universe)


(initget 2048)
Prevents the user from responding to the request by entering non-numeric values at getstring prompt


 8)

efernal

  • Bull Frog
  • Posts: 206
Re: How to force user input 5 digits?
« Reply #7 on: February 13, 2013, 08:22:40 AM »
another...

Code - Auto/Visual Lisp: [Select]
  1. (DEFUN c:test (/ m_str)
  2.   (WHILE (NOT (WCMATCH (SETQ m_str (GETSTRING "\n-> Type 5 digits : ")) "#####")))
  3.   (SETQ m_str (STRCAT "B" m_str))
  4.   (ALERT m_str)
  5. )
  6.  
e.fernal

MeasureUp

  • Bull Frog
  • Posts: 462
Re: How to force user input 5 digits?
« Reply #8 on: February 13, 2013, 07:00:24 PM »
Big Thanks to all your helps.
In my case, "B#####" is the simple sulotion.
And all your helps are Brilliant, surely.

BTW, I have wrote more than hundred lines by looking at "acsii"...

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: How to force user input 5 digits?
« Reply #9 on: February 13, 2013, 07:02:21 PM »
Code: [Select]
(wcmatch str "B#####")

... Brilliant.

Cheers dude  :-)