Compare numeric strings with leading zeros

Lyn Teyla lyn.teyla at gmail.com
Thu Sep 3 16:34:43 EDT 2015


Thierry Douez wrote:

> on mouseUp
>   local userTyping = 5
>   local myVeryStrongPassword = "005"
>   if matchText( userTyping, myVeryStrongPassword) then
>      answer "Great!"
>   else
>      answer "Too bad :( try again.."
>      put "005" into userTyping
>      if matchText( userTyping, myVeryStrongPassword) then answer "Great!"
>   end if
> end mouseUp

I would caution against using matchText for this purpose, because the second parameter is treated by the function as a regular expression.

For instance, matchText would return true if you were to reverse your example values:

   local userTyping = "005"
   local myVeryStrongPassword = 5

This is because 005 does indeed contain 5.

In addition, since passwords are typically allowed to contain any character, including those that have special meaning in regular expressions, something like this would also return true:

   local userTyping = "5"
   local myVeryStrongPassword = "^5$"

With this in mind, I would go with the method of first checking the length followed by the values as suggested by a couple of previous posters.

Since Ralph is looking to use this for password validation, I would throw in a case sensitivity check as well:

on mouseUp
   put stringsAreEqual("005", "5")
end mouseUp

function stringsAreEqual pString1, pString2
   set the caseSensitive to true
   if (len(pString1) = len(pString2)) and (pString1 = pString2) then
      return true
   end if
   return false
end stringsAreEqual


Hope this helps!

Lyn






More information about the Use-livecode mailing list