removing unwanted characters from a field,variable, or file

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
kcwvc52
Posts: 49
Joined: Sun Jun 12, 2011 9:55 pm

removing unwanted characters from a field,variable, or file

Post by kcwvc52 » Wed Aug 03, 2011 9:36 pm

i have searched documentation and the forums and haven't found a good way to remove unwanted characters from a field. so when a button is clicked i want to remove all punctuation from the field or even better would be to keep users from being able to input non-alphanumerics into a field. does livecode have any simple code i'm missing to handle this? also i would like to note im coding for ios. i know i can choose the keyboard,but that does not keep users from typing things that are not wanted.

townsend
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 430
Joined: Sun Feb 13, 2011 8:43 pm

Re: removing unwanted characters from a field,variable, or file

Post by townsend » Wed Aug 03, 2011 10:13 pm

Here's a good routine you can place in the code for the field.
This prevents all numeric input AND restricts length to 12 or less.

Code: Select all

on keydown inkey
     if inkey is in "1234567890"  then
          beep
          exit to top
     end if
     if the length of field "someInput" <=12 then
          pass keydown
     else
          beep
     end if
end keydown

kcwvc52
Posts: 49
Joined: Sun Jun 12, 2011 9:55 pm

Re: removing unwanted characters from a field,variable, or file

Post by kcwvc52 » Wed Aug 03, 2011 10:32 pm

i don't mind numbers what i'm really trying to restrict is any punctuation and unaccepted characters which with that code is fairly easy except for " cause livecode thinks i'm closing what i want in quotes. Another problem i had with this is that it won't type anything in the field? but it does beep when i typed a number of course

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: removing unwanted characters from a field,variable, or file

Post by dunbarx » Wed Aug 03, 2011 10:51 pm

Townsend's script likely pointed the way for you, but to prevent the entry of all non-alphanumeric characters, you might need:

Code: Select all

on keydown inkey
      if inkey is in "0123456789abcdefghijklmnopqrstuvwxyz"  then
      pass keydown
       end if
end keydown
Check out the function "charToNum" and "numToChar". These will make you think about other ways to catch certain chars.

Code: Select all

on keydown inkey
      if (chartonum(inkey) >= 65 and  chartonum(inkey) <= 90) or\
   (chartonum(inkey) >= 97 and  chartonum(inkey) <= 122) or\
   (chartonum(inkey) >= 48 and  chartonum(inkey) <= 57)
   then
      pass keydown
   end if
end keydown
The last is verbose, but has the advantage of not being run-on. Anyway, it shows another method.

Note that the delete key works here. Try this with a "rawKeyDown" handler instead of a "keyDown:

Code: Select all

on rawkeydown inkey
      if inkey >= 65 and  inkey <= 90 or\
   inkey >= 97 and  inkey <= 122 or\
   inkey >= 48 and  inkey <= 57
   then
      pass rawkeydown
   end if
end rawkeydown
What's with "delete"?

Do you still need a clean-up handler to get rid of all non-alphanumeric chars in an existing field?

Is there something special about 12 characters, perhaps something pertinent to iOS?

Craig Newman

kcwvc52
Posts: 49
Joined: Sun Jun 12, 2011 9:55 pm

Re: removing unwanted characters from a field,variable, or file

Post by kcwvc52 » Wed Aug 03, 2011 11:22 pm

thanks that first script does exactly what i was looking for. and on the amount of characters, i wasn't concerned with that but it is good to know now. I am still concerned with removing it from files being input into files. if anyone has a simple ways for removing it from files it would be much appreciated.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: removing unwanted characters from a field,variable, or file

Post by dunbarx » Thu Aug 04, 2011 12:27 am

So that is a "yes", you need to extract only alphaNumeric data from existing data? Easy! If you have a field "yourField":

Code: Select all

on mouseup
   get fld "yourField"
put "0123456789abcdefghijklmnopqrstuvwxyz" into tAlpha
   repeat for each char tChar in it
      if tchar is in tAlpha then put tchar after temp
   end repeat
   put temp into fld "yourField"
end mouseup
You don't want spaces, right?

Craig Newman

kcwvc52
Posts: 49
Joined: Sun Jun 12, 2011 9:55 pm

Re: removing unwanted characters from a field,variable, or file

Post by kcwvc52 » Thu Aug 04, 2011 3:05 am

thanks that works great. and i actually don't mind spaces but that is an easy fix just add a space at the like this "0123456789abcdefghijklmnopqrstuvwxyz " and it worked fine for me.

Post Reply