Number of chars

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

Post Reply
Quinton B.
Posts: 108
Joined: Mon Mar 20, 2017 5:13 am

Number of chars

Post by Quinton B. » Sat Aug 10, 2019 11:04 am

Good day,

I've been trying to use the following code:

Code: Select all

 get the number of chars of field "RegisterPassword"
in order to gather how many characters are within the field (eg. A,B,C/etc) and

Code: Select all

get the number of nums of field "RegisterPassword"
in order to gather how many numbers are within the field.

What should I do, or try to do since this is an invalid statement?

Klaus
Posts: 13828
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Number of chars

Post by Klaus » Sat Aug 10, 2019 11:48 am

Hi Quinton,

maybe some nifty REGEX will solve this, but sincxe I still have not the slightest idea about
that stuff, I would probably use a "repeat for each char tChar...if tChar is a number then
add 1 to tNumbers..." loop. :D


Best

Klaus

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9834
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Number of chars

Post by FourthWorld » Sat Aug 10, 2019 5:17 pm

What is the total length of the field contents, and is there any structure to the placement of numbers in the text?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

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

Re: Number of chars

Post by [-hh] » Sat Aug 10, 2019 6:13 pm

Quinton,
after reading your question a second time I think you look for counting the number of digits 0-9 rather than "numbers", in order to check for a certain "password-rule".

Here is a stack that does even more such password rules: viewtopic.php?p=182110#p182110.

And you could only count the number of digits as follows (similar to what Klaus said):

Code: Select all

function numOfDigits str
   put 0 into cntr
   repeat for each char c in str
      if c is in "0123456789" then add 1 to cntr
   end repeat
   return cntr
end numOfDigits
shiftLock happens

Quinton B.
Posts: 108
Joined: Mon Mar 20, 2017 5:13 am

Re: Number of chars

Post by Quinton B. » Sat Aug 10, 2019 9:28 pm

I believed I've confuzzeled myself.

My criteria is for the password to be 10 chars long, 4 chars (2 Capital and 2 Lower Case), 4 numbers, and 2 Special Characters.

I've tried already to modify your code but honestly get lost as I don't really know how it works:

Code: Select all

function numOfDigits str
   put 0 into cntr
   repeat for each char c in str
      if c is in "0123456789" then add 1 to cntr
   end repeat
   return cntr
end numOfDigits

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

Re: Number of chars

Post by [-hh] » Sat Aug 10, 2019 10:50 pm

You could use stack "passwordRules" from "Sample Stacks" with the following settings
(where fld "Xxtra" contains your allowed special chars).
 
sample.png
Ready for input of a password to check.
Then you can check whether a given password fulfils your rule or you can generate a random password that fulfils your rules.
 
generated.png
Generated random password.
What you need for use in your stack is the script of btn "Check" (the options and the fields hardcoded) or, for generating, the script of btn "Generate" (the options and the fields hardcoded).
shiftLock happens

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”