I’m not hugely experienced in coding and so much better at editing existing code than creating it from scratch so this one has been a bit much for me to figure out!
I am trying to create a user input field which acts as a postcode (zipcode) validation. I am having trouble combining if and contains functions.
The way a UK postcode is structured is in two parts eg. G1 1BB, where “G1” is the area code and “1BB” refers to a specific street. I am only supplying my services to specific area codes and therefore need to check if the user input contains “G1” “G2” “G3” … “G81”
I am trying to write code where a user inputs their postcode and clicks a button which will validate their entry if it contains a stated area code and show a second button or reject their entry with a message.
I wont attempt to write this in code as every experiment I have done has been a failure, but I’ll write it out in english to show the structure I am trying to create.
User inputs postcode into #input1 and clicks #button1
button1.click
if #input1 value contains “G1”
#button2.show
else if #input1 value contains “G2”
#button2.show
else if #input1 value contains “G3”
#button2.show
…
else if #input1 value contains “G81”
#button2.show
else
#text1.show
I know the above is nonsense but I don’t have the coding proficiency to write it out anywhere near correctly!
Does anyone have an idea of how I can successfully do this and combine if and contains functions?
Hi, your logic is correct, you just need the syntax. It’s just like a new language.
To avoid using a bunch of IFs and make your life easier, you can put every G## that you provide service into an array, like:
const arrayOfZipCodesAllowed = [
"G1",
"G2",
"G3",
"G4",
"G81"
]
Then, you can check if the user input includes any of this and return true or false, like this:
$w("#buttonVerify").onClick(() => {
const searchZipCode = $w("#inputZipCode").value //Puts the input value inside a variable
const zipCodeCheckResult = zipCodeCheck(searchZipCode)
if (zipCodeCheckResult) $w("#button2").show()
else $w("#text1").show()
})
function zipCodeCheck(zipCode) {
const checkArrayResult = arrayOfZipCodesAllowed.some(item => item.includes(zipCode))
return checkArrayResult
}
Thanks so much! I’ll try a few more experiments based on this and try and implement an array.
Would the method you are using be able to tell if a user input contains an approved post(zip)code, rather than look for an exact match?
I would expect a user to input a full postcode ie. “G1 1BB” but I would only need the code to approve it based on it containing the area code “G1”. If it were looking for an exact match, I would literally have thousands of items to enter into the array… not impossible but not pleasant either!
There are to methods being used in the zipCodeCheck() function, they are .some() and .includes() .
.some() is a native method that check for items in an array. It goes through the array items, trying to find whatever you tell it to find, and returns either true or false .
.includes() is a method that checks if a collection of characters is included in a string, partially or not, and also returns true or false .
So the function mentioned above goes from item to item in the array, and on each item, tries to find the string defined in the input.
Upon writing this message, I noticed an error, you should change something to include partial results. This is the correct version:
$w("#buttonVerify").onClick(() => {
const searchZipCode = $w("#inputZipCode").value //Puts the input value inside a variable
const zipCodeCheckResult = zipCodeCheck(searchZipCode)
if (zipCodeCheckResult) $w("#button2").show()
else $w("#text1").show()
})
function zipCodeCheck(zipCode) {
const checkArrayResult = arrayOfZipCodesAllowed.some(item => zipCode.includes(item)) //This was inverted on the code.
return checkArrayResult
}
Thanks so much for your help!
I’m trying to implement it just now but still not getting any results back - I’ll keep trying with this method and see if I can crack it. 
@markjamesstockton if post your code, I can check it for you.