Skip to content
⌘ NSIS Forum Archive

String length

21 posts

MyPC8MyBrain#

String length

i am trying to verify if an input field has at least X number of characters in it,
i need to replace this line in the script below this line ${CheckUserEmailAddress} "$1" "$R1"
with an input string length check; instead of email check i have for for the second field (its not a password field!)

after the email is verified; i just need to verify the next field has at least 8 characters before moving on to the next check,

any advice?

TIA
Chris

Function CustomCreate
    nsDialogs::Create 1018
    Pop $Dialog2
    ${If} $Dialog2 == error
        Abort
    ${EndIf}
        ${NSD_CreateGroupBox} 5u 0u 96.5% 55u "Order Details"
        Pop $GroupBox1
        SendMessage $GroupBox1 ${WM_SETFONT} $hCtl_TestDesigner_Font1 0        
        ${NSD_CreateLabel} 15 31 20% 12u "Email:"
        Pop $0
        ${NSD_CreateText} 120 26 50% 13u $Text2_State
        Pop $Text
        ${NSD_CreateLabel} 15 55 20% 12u "Code"
        Pop $1
        ${NSD_CreateText} 120 50 50% 13u $Text2_State
        Pop $Text2
        nsDialogs::Show
FunctionEnd
Function CustomLeave
         ${NSD_GetText} $Text $0
             ${CheckUserEmailAddress} "$0" "$R0"
             StrCmp $R0 "1" 0 next
             MessageBox MB_OK "invalid email"
             Abort
         next:
         ${NSD_GetText} $Text2 $1
             ${CheckUserEmailAddress} "$1" "$R1"
             StrCmp $R1 "1" 0 next2
             MessageBox MB_OK "invalid code"
             Abort
         next2:
FunctionEnd 
JasonFriday13#
http://nsis.sourceforge.net/Docs/Chapter4.html#StrLen.
http://nsis.sourceforge.net/Docs/Chapter4.html#intcmp.
MyPC8MyBrain#
Thank you Jason,
i saw that earlier; i couldn't figure how to incorporate it with length check,
i wasn't sure if its the right way about this; ill give it another go,

ATB
Chris
MyPC8MyBrain#
Thank you Jason 🙂
what am i missing here?

         ${NSD_GetText} $Text2 $1
             StrLen $1 "11111111" # =8
             Pop $R1
             IntCmp $R1 8 ok less more
             less:
             DetailPrint "$$R1 < 8"
             MessageBox MB_OK "invalid"
             Abort
             more:
             DetailPrint "$$R1 > 8"
             Goto done
             ok:
             DetailPrint "$$R1 == 8"
             Goto done
             done: 
MyPC8MyBrain#
in this way it all pass valid blindly

         ${NSD_GetText} $Text2 $1
             StrLen $1 "11111111" # =8
             Pop $1
             IntCmp $1 8 ok less more
             less:
             DetailPrint "$$1 < 8"
             MessageBox MB_OK "invalid"
             Abort
             more:
             DetailPrint "$$1 > 8"
             Goto done
             ok:
             DetailPrint "$$1 == 8"
             Goto done
             done: 
Anders#
After 'StrLen $1 "11111111"' $1 is always going to be 8. You are retrieving the text with ${NSD_GetText} and then throw away this text on the next line when it is overwritten by the result of StrLen. The syntax of StrLen is: StrLen $Output "Input", if you cannot understand this basic instruction then you might want to take a look at other installers like Inno Setup with less focus on pure scripting...
MyPC8MyBrain#
Thank you Andres,
that is exactly what i was seeing earlier; i was always getting 8 for result,
i had to walk away for a bit as i was getting frustrated with this not working for me;

but... after rebooting my system i finally got it right 🙂

             ${NSD_GetText} $Text2 $1
             StrLen $R1 "$1"
             StrLen $R2 '11111111'
             IntCmp $R1 $R2 ok less more
             less:
             MessageBox MB_OK "Invalid"
             Abort
             more:
             Goto done
             ok:
             Goto done
             done: 
im still a newbie struggling with allot to pickup in a very short amount of time,
it is challenging; but i am learning, you and Jason been great resource throughout this learning curve,

ATB
Chris
JasonFriday13#
You can optimize this even more by removing the second StrLen:
${NSD_GetText} $Text2 $1
StrLen $R1 "$1"
IntCmp $R1 8 ok less more
less:
MessageBox MB_OK "Invalid"
Abort
more:
Goto done
ok:
Goto done
done: 
MyPC8MyBrain#
quick question,
my nsDialogs seem to get stuck after submit comes with error,
(from incorrect email address or something expected)
the dialog input becomes locked for editing; i have to go back a page then forward again;
why is that? am i missing a refresh or something?

TIA
Chris
JasonFriday13#
Really? See if this works for you (it works for me):

Name "Test"
OutFile "test.exe"
RequestExecutionLevel User
!include "MUI2.nsh"
Var Dialog2
Var GroupBox1
Var Text
Var Text2
page custom CustomCreate CustomLeave
!insertmacro MUI_PAGE_COMPONENTS
!insertmacro MUI_LANGUAGE "English"
Section
SectionEnd
Function CustomCreate
  nsDialogs::Create 1018
  Pop $Dialog2
  ${If} $Dialog2 == error
    Abort
  ${EndIf}
  ${NSD_CreateGroupBox} 5u 0u 96.5% 55u "Order Details"
  Pop $GroupBox1
  ${NSD_CreateLabel} 15 31 20% 12u "Email:"
  Pop $0
  ${NSD_CreateText} 120 26 50% 13u "1"
  Pop $Text
  ${NSD_CreateLabel} 15 55 20% 12u "Code"
  Pop $1
  ${NSD_CreateText} 120 50 50% 13u "2"
  Pop $Text2
  nsDialogs::Show
FunctionEnd
Function CustomLeave
  ${NSD_GetText} $Text $0
  MessageBox MB_OK "invalid email, |$0|"
  ${NSD_GetText} $Text2 $1
  MessageBox MB_OK "invalid code, |$1|"
  Abort
FunctionEnd 
Just on another note, also learn how to indent your code properly, it makes it easier to read. So my example would look like this:

Function something
  ${NSD_GetText} $Text2 $1
  StrLen $R1 "$1"
  IntCmp $R1 8 ok less more
  less:
    MessageBox MB_OK "Invalid"
    Abort
  more:
    Goto done
  ok:
    Goto done
  done:
FunctionEnd 
MyPC8MyBrain#
Thank Jason 🙂
points taken!

the code you posted works ok for me too;
the issue i believe is with next step i have in the function,

i send the input out; if it passes your off to the next page;
if response comes back invalid; the input become locked,

inetc::POST "" "http://..."
  Pop $1
  FileOpen $2 $0 "r"
  FileRead $2 $3
  FileClose $2
${If} $3 == 'INVALID'
    MessageBox MB_OK|MB_ICONEXCLAMATION "Invalid"
    IntOp $count $count + 1
    IntCmp $count 3 0 continue 0
    MessageBox MB_OK|MB_ICONSTOP "Invalid, Installer will  now exit."
    quit
  continue:
    Abort
${EndIf} 
(yes, there is a counter var on OnInit)
i think i need to sent it back to the previous page after failure;
but thats on page create; and this is on page leave,
i have nothing on leave page to restart the loop from again,
JasonFriday13#
So which function is that code in? It's unwise to use abort within a Create function. Putting up the leave function and the next page's create function will help greatly.
MyPC8MyBrain#
Thank you very much Jason,

that bit is the second part of - on page leave, its not on page create;

i take the email input and code; then submit it for verification online,
the return token is then compared for false positive with that bit;
all still within page leave function,

the abort is there to end the loop and go again if the local basic input check fails,

ATB
Chris
JasonFriday13#
Ok, you will have to do some troubleshooting. Start commenting out commands one at a time until it works, then start uncommenting them one at a time until it fails. This will lead to the command that's causing it to fail. Once you can control a failure, then you can find a solution to it.
MyPC8MyBrain#
i know where the issue is; its with continue: -> Abort
instead of aborting i need it to goto the start again;
but aborting there doesn't send me to start because im already on page leave instructions,