removing unwanted characters from a field,variable, or file
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
removing unwanted characters from a field,variable, or file
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.
Re: removing unwanted characters from a field,variable, or file
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.
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
Re: removing unwanted characters from a field,variable, or file
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
Re: removing unwanted characters from a field,variable, or file
Townsend's script likely pointed the way for you, but to prevent the entry of all non-alphanumeric characters, you might need:
Check out the function "charToNum" and "numToChar". These will make you think about other ways to catch certain chars.
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:
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
Code: Select all
on keydown inkey
if inkey is in "0123456789abcdefghijklmnopqrstuvwxyz" then
pass keydown
end if
end keydown
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
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
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
Re: removing unwanted characters from a field,variable, or file
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.
Re: removing unwanted characters from a field,variable, or file
So that is a "yes", you need to extract only alphaNumeric data from existing data? Easy! If you have a field "yourField":
You don't want spaces, right?
Craig Newman
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
Craig Newman
Re: removing unwanted characters from a field,variable, or file
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.