Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

test numeric data in text field

Status
Not open for further replies.

japskar

Programmer
Feb 13, 2002
27
Hi,
I'm trying to create a text widget (multiline) in which a user can only enter numeric data,blanks,tabs and newlines.

Does anyone know a way of testing such a textwidget for the above data?

 
You could create a binding to one or more events on the text widget to test for the various "legal" characters, and break if it isn't a legal character. But this doesn't prevent a user from pasting in a bunch of text that could contain illegal characters.

Instead, the best way of accomplishing your goal is to in essence "intercept" all character insertions into your text widget. This is a fairly common Tcl technique, and is described at the Tcl'ers Wiki ( on the page "Overloading widgets,"
What we're going to do is provide a "wrapper" to the standard widget command that's created when you create the text widget. Then, whenever we detect an insert operation, we'll filter out all the "illegal" characters. This works because all character insertions are ultimately performed by the insert operation, even if they are the result of keystrokes or pastes, because of the way the standard text widget bindings are written. Here's an example that does what you requested (please excuse any bad line breaks):

Code:
text .t

# Hide the default widget command

rename .t _.t

# Create our wrapper command

proc .t {cmd args} {
  switch -- $cmd {
    insert  {
      # Filter text we're going to insert
     
      # Copy the index into the new argument list
     
      set newargs [lindex $args 0]
     
      # The remaining arguments should be text
      # followed by an optional tag(s) argument,
      # possibly repeated multiple times.
     
      set index 0
      foreach arg [lrange $args 1 end] {
        incr index
       
        # Filter only the text, not the tags
       
        if {($index % 2) == 1} {
       
          # Delete anything other than digits,
          # spaces, tabs, and newlines.
         
          regsub -all -- "\[^0-9 \t\n]" $arg "" arg
        }
        lappend newargs $arg
      }
     
      # Call the hidden text widget command with
      # our filtered arguments.
     
      uplevel 1 _.t insert $newargs
    }
    default {
      # Anything other than an insert operation we
      # pass to the hidden text widget command untouched.
     
      uplevel 1 _.t $cmd $args
    }
  }
}

pack .t
- Ken Jones, President
Avia Training and Consulting
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
thanx a lot!
I'll be on my way with this.

But what happens if someone enters a number like 3e7 ?
Or 100.000,5 ?
Or 3.1?

For I can imagine that these are commonly made mistakes.

-Jasper van Dijk
 
Actually, thinking about this solution, I could just as well use this technique to check for an tab/return/space insertion, take the characters untill the previous tab/space/return, and test it, which is more convenient for me, because now I can pass this data to APL and test it there.

Thanx a lot.
I owe you...again Mr. Jones.

best regards,
-Jasper van Dijk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top