We recently modified a database field so that a single user record can be assigned multiple types in a comma delimited list. So instead of UserType=1, for our condition, we need to check if Session("UserType") contains 1 in a comma delimited list.
assuming users will have values like 1,7,8 or 1,3,7 how can I scan and confirm a "contains" condition in asp vb?
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
<%test="1,3,7,9,10" if checkIT(test)=1 then%><div class="nav">Nav bar</div><%end if%><%' functions are typically at the top or bottom of your page.function checkIT(user_type) checkIT=0 arrUsertype=split(user_type,",") for each number in arrUsertype if cstr(number)="1" then checkIT=1 end if nextend function%>
Hi,
I would go the route with padas solution BUT would extend it a little bit - just to pass the check parameter as well:
<%test="1,3,7,9,10" if checkIT(test,"1") OR checkIT(test,"7") then%><div class="nav">Nav bar</div><%end if%><%' functions are typically at the top or bottom of your page.function checkIT(user_type,valueToCheck) checkIT=false arrUsertype=split(user_type,",") for each number in arrUsertype if cstr(number)=cstr(valueToCheck) then checkIT=true end if nextend function%>
Thanks All - now Gary's solution is clearly the simplest, and right now all values are 1 - 8, but just to clarify, the second example assumes the number MUST be greater than 9?
So truly if we get to 10 user types, we need to use Pradas or Rainer's solutions?
Hi,
Gary`s is the quickest one - but hard to read and you have always to add the "," before and after your session variable. This will work for any number but it could be not as error save as Padas/my solution due to the extras you have to add.
Padas solution is the right one in terms of best practices as it creates the array with the distinct values and then runs the compare.
My extension to Padas solution just make it usable for any check you want to run - check for 5 or 10 or ...
instr(Session("UserType"),
If greater than 9 then
instr(","&Session("UserTyp