Pattern matching

Hey guys,

I think what I want to do is match a pattern, I can’t quite seem to get my head around how it works.

My app needs an 8 digit number entered for a loyalty card.

The first 4 digits never change e.g. 2222

So I need to ask people to enter a number. I have done this and assigned the value to a variable.
 

The bit I need help with is the validation of that value.

It needs to be always 2222 + xxxx (where xxxx is the changeable part of the loyalty card number)

Looking forward to some advice.

Thanks

I think we need to know a bit more. Where are the “valid” IDs held? Presumably on a server?  

You would need to send the ID generated by the user to your server, and run some SQL (or whatever database language you’re using) to see if that ID is found.

Also, you’d really need to have some kind of encryption so that other people can’t send the same details as another user (if the first 4 digits are always the same then you’ve got a range of 0000 - 9999 which is not very big).

If however the valid ID is held on the device for some reason, then you can just check if the 2 strings are the same:

--validID is the ID you want to match against --enteredID is the variable you've already told us about if tostring(validID) == tostring(enteredID) then print("success") else print("fail") end

I’ve used tostring() in case one ID is a number and the other is a string, otherwise they won’t match.

e.g. 1234 is not the same as “1234”

Sorry. The app is for a very small cafe and the number is printed on the back of their loyalty card. It does not connect to a server in anyway, just saves locally within a text file. It is a very basic app for ordering.

the owner doesn’t expect anymore than 9999 cards to be given out. I hope that helps. he isn’t really bothered if more than one user enters the same loyalty number.

Hope that helps.

I’m not sure you need pattern matching here, just a couple of string API calls:

local prefix = string.sub(loyaltyCode, 1, 4)

local suffix = string.sub(loyaltyCode, 5)

local loyaltyNumber

if prefix == “2222” and suffix:len() == 8 then

     loyaltyNumber = to.number(suffix)

end

or something like that.

Rob

I think we need to know a bit more. Where are the “valid” IDs held? Presumably on a server?  

You would need to send the ID generated by the user to your server, and run some SQL (or whatever database language you’re using) to see if that ID is found.

Also, you’d really need to have some kind of encryption so that other people can’t send the same details as another user (if the first 4 digits are always the same then you’ve got a range of 0000 - 9999 which is not very big).

If however the valid ID is held on the device for some reason, then you can just check if the 2 strings are the same:

--validID is the ID you want to match against --enteredID is the variable you've already told us about if tostring(validID) == tostring(enteredID) then print("success") else print("fail") end

I’ve used tostring() in case one ID is a number and the other is a string, otherwise they won’t match.

e.g. 1234 is not the same as “1234”

Sorry. The app is for a very small cafe and the number is printed on the back of their loyalty card. It does not connect to a server in anyway, just saves locally within a text file. It is a very basic app for ordering.

the owner doesn’t expect anymore than 9999 cards to be given out. I hope that helps. he isn’t really bothered if more than one user enters the same loyalty number.

Hope that helps.

I’m not sure you need pattern matching here, just a couple of string API calls:

local prefix = string.sub(loyaltyCode, 1, 4)

local suffix = string.sub(loyaltyCode, 5)

local loyaltyNumber

if prefix == “2222” and suffix:len() == 8 then

     loyaltyNumber = to.number(suffix)

end

or something like that.

Rob