Limiting data entry in fields
Jan Schenkel
janschenkel at yahoo.com
Mon Nov 29 23:46:49 EST 2010
--- On Mon, 11/29/10, Monte Goulding <monte at sweattechnologies.com> wrote:
> Hi Charles
>
> I'm a little confused about your requirements. Did you want
> to allow decimals or not? Here's some code copied from one
> of my behavior scripts. You only need to modify the one
> function to impose whatever data entry limits you like:
>
> on keydown pKey
> if checkText(getNewText(pKey)) then
> setNewText pKey
> else
> beep 1
> end if
> end keyDown
>
> on pasteKey
> if checkText(getNewText(the
> clipboardData["text"])) then
> paste
> else
> beep 1
> end if
> end pasteKey
>
> # This is the only function you need to change to vary the
> acceptable data entry
> private function checkText pText
> return pText is an integer
> end checkText
>
> private function getNewText pNewChars
> local tEndChar,tStartChar,tText
> put word 2 of the selectedchunk of me
> into tStartChar
> put word 4 of the selectedchunk of me
> into tEndChar
> put the text of me into tText
> put pNewChars into char tStartChar to
> tEndChar of tText
> return tText
> end getNewText
>
> private command setNewText pNewChars
> local tEndChar,tStartChar,tText
> put word 2 of the selectedchunk of me
> into tStartChar
> put word 4 of the selectedchunk of me
> into tEndChar
> put pNewChars into char tStartChar to
> tEndChar of me
> end setNewText
>
> Cheers
>
> Monte
>
Don't forget dragDrop :-)
Seriously, this is an old question that keeps coming back, and a prime candidate for either an engine-level field "mask", or a community effort to write a behavior script that handles the corner cases and is readily available for download.
A bit of searching turned up bits and pieces as far back as 2002 in my archives, but the combination of these two emails seems to do the trick:
<http://lists.runrev.com/pipermail/use-livecode//2002-October/008411.html>
<http://lists.runrev.com/pipermail/use-livecode//2003-September/022375.html>
(best to follow these links, as it looks like the post-rename archives are slightly misaligned)
While it does a bit too much for Charles' use case (formatting number for display) the underlying logic is sound. Thanks to Ken Ray for writing the RegEx back in 2002 to split a number into a sign, integer and decimal part.
<http://lists.runrev.com/pipermail/use-livecode//2002-September/008030.html>
Jan Schenkel.
=====
Quartam Reports & PDF Library for LiveCode
www.quartam.com
=====
"As we grow older, we grow both wiser and more foolish at the same time." (La Rochefoucauld)
More information about the use-livecode
mailing list