check field for letters

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

DavJans
Posts: 275
Joined: Thu Dec 12, 2013 4:21 pm

check field for letters

Post by DavJans » Tue Oct 28, 2014 9:29 pm

I have found lots of help on what I need but all seem to want me to use pKey. if pKey is not a number then ...
I think this will not work for me because I have buttons that auto fill fields with text. Maybe I'm wrong.

What I would like is to check the field AFTER data entry for letters.

if fld "field 1" contains "a-z" then
answer "no letters allowed"

is this doable?

Thank you in advance and you are all awesome :)
"Det bästa stället att hitta en hjälpande hand är i slutet av din egen arm" förutom här

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

Re: check field for letters

Post by dunbarx » Tue Oct 28, 2014 10:49 pm

Hi.

Why not loop through all the chars in the field and eliminate those that are in "abcd...z"? Case would not matter, which is a benefit. And "repeat for each..." is really, really fast. Write back if you get stuck, but this should be fun.

Craig Newman

Edit, upon rereading, I see what you really asked for. Now you would loop through all 26 chars, and stop if even one of them was in the source text. Faster yet, the size of the source now being irrelevant. The first post would be a way to delete the offending chars.

I assume there is a short regex solution as well.

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: check field for letters

Post by [-hh] » Wed Oct 29, 2014 11:25 am

The following uses a regular expression.

Code: Select all

on mouseUp
  if matchtext(fld 1 ,"[a-zA-Z]")
  then answer "No letters (a-z or A-Z) allowed."
end mouseUp
Warning: This doesn't find international chars (like "ü").
shiftLock happens

DavJans
Posts: 275
Joined: Thu Dec 12, 2013 4:21 pm

Re: check field for letters

Post by DavJans » Wed Oct 29, 2014 4:31 pm

Great step in the right direction I think, but of course there is another problem. Here is how I used your code.

on closeField
if matchtext(fld "welder" ,"[a-zA-Z]") then
answer "No letters (a-z or A-Z) allowed."
focus on fld "welder"
BREAK
end if
end closeField

However its not working right, It wont refocus on the field as I expected and It is running the openField script on the next field selected.
"Det bästa stället att hitta en hjälpande hand är i slutet av din egen arm" förutom här

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: check field for letters

Post by [-hh] » Wed Oct 29, 2014 5:47 pm

You could try:

Code: Select all

-- in card's script (for all fields on card)
-- or in field's script (for that field only)
on closeField
  if matchtext(text of the target,"[a-zA-Z]") then 
    answer "No letters (a-z or A-Z) allowed."
    select text of the target
  else pass closeField
end closeField
shiftLock happens

MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Re: check field for letters

Post by MaxV » Wed Oct 29, 2014 5:56 pm

The best solution is: 8)
########CODE#######
on keyDown pKey
if pKey is not a number and pKey is not "." then
beep
else
pass keyDown
end if
end keyDown
#####END OF CODE#####
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: check field for letters

Post by [-hh] » Wed Oct 29, 2014 6:06 pm

Depends upon what he wants.
For example space, minus, plus, parentheses, brackets are also allowed with phone numbers.
Or: Numbers and "." are *not* the complement of a-z & A-Z :-)
shiftLock happens

MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Re: check field for letters

Post by MaxV » Wed Oct 29, 2014 6:23 pm

[-hh] wrote:Depends upon what he wants.
For example space, minus, plus, parentheses, brackets are also allowed with phone numbers.
Or: Numbers and "." are *not* the complement of a-z & A-Z :-)
If you need more, just add it: :lol:
########CODE#######
on keyDown pKey
if pKey is not a number and pKey is not in "+-.,()[]{}" then
beep
else
pass keyDown
end if
end keyDown
#####END OF CODE#####
However you solution is better with autofill button.
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

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

Re: check field for letters

Post by dunbarx » Wed Oct 29, 2014 6:28 pm

The OP wanted to validate AFTER the field had been populated. That is, all at once at some later time. Any solution that uses "keydown" is validating on the fly.

Craig

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: check field for letters

Post by [-hh] » Wed Oct 29, 2014 6:46 pm

@MaxV.
It's often advantageous to consider what is simpler to enumerate, a set or its complement.
May be you are right. But just to keep you busy with beeping:
Now assume he wants to have some words containing *only* a-z or A-Z to be typed (for example for a userName).
shiftLock happens

DavJans
Posts: 275
Joined: Thu Dec 12, 2013 4:21 pm

Re: check field for letters

Post by DavJans » Wed Oct 29, 2014 7:05 pm

-- in card's script (for all fields on card)
-- or in field's script (for that field only)
on closeField
if matchtext(text of the target,"[a-zA-Z]") then
answer "No letters (a-z or A-Z) allowed."
select text of the target
else pass closeField
end closeField
That did it, almost, Had to Add a break after select text of.. and i had to change the target to fld "welder" then it worked perfect, thank you all for your help.
"Det bästa stället att hitta en hjälpande hand är i slutet av din egen arm" förutom här

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: check field for letters

Post by [-hh] » Wed Oct 29, 2014 7:23 pm

Now all joking aside.

What's the shortest way to have a check that a string contains only unicode 'letters' (incl. internatonal diacritics and 'twobyte' chars)?

Is "Mr. Regex" back from holidays?
shiftLock happens

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: check field for letters

Post by Simon » Wed Oct 29, 2014 10:44 pm

I did see Mr. Regex in here earlier today.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: check field for letters

Post by jacque » Thu Oct 30, 2014 5:06 pm

The "break" command is only for use in switch statements. If it works here, it's by accident and I wouldn't rely on it. You shouldn't need it anyway, the "if" clause should handle everything by itself.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

DavJans
Posts: 275
Joined: Thu Dec 12, 2013 4:21 pm

Re: check field for letters

Post by DavJans » Thu Oct 30, 2014 5:55 pm

I found that If using the mouse to click into another field, it will give the message about no letters, but it will still move to the clicked on field leaving the field with illegal characters :(
"Det bästa stället att hitta en hjälpande hand är i slutet av din egen arm" förutom här

Post Reply