I have a simple if/then/else and it’s throwing errors. I know i’m rusty, but come on.
If txtRegFullName = ""
NSB.MsgBox("Please enter a full name")
Else
NSB.MsgBox(txtRegFullName)
End If
Please tell me what I am doing wrong here? I’m just trying to do some simple error correction and to see how to get the value of the text input to show in the messagebox. I am trying to encrypt the text entered into the control and then show the encrypted text back into the same text input control.
This is the new version with encryption and stuff - please help me correct this so I know how to do this for all inputs:
If txtRegFullName.value = ""
NSB.MsgBox("Please enter a full name")
Else
cryptRegFullName = sjcl.encrypt(txtPassword, txtRegFullName.value)
NSB.MsgBox("Text is: " & txtRegFullName.value & " - Encrypted text is: " & cryptRegFullName.value)
End If
If txtRegFullName = ""
NSB.MsgBox("Please enter a full name")
Else
cryptRegFullName = sjcl.encrypt(txtPassword, txtRegFullName)
NSB.MsgBox("Text is: " & txtRegFullName & " - Encrypted text is: " & cryptRegFullName)
End If
If txtRegFullName = ""
NSB.MsgBox("Please enter a full name")
Else If
cryptRegFullName = sjcl.encrypt(txtPassword, txtRegFullName)
NSB.MsgBox("Text is: " & txtRegFullName & " - Encrypted text is: " & cryptRegFullName)
End If
So this does not work as far as error correction - if I leave the text input (jqWidget) blank, no message box appears. If I input a name, I get undefined errors:
Uncaught TypeError: Cannot read property 'slice' of undefined
at Object.encrypt (code.js:69)
at Object.encrypt (code.js:87)
at HTMLButtonElement.btnRegGo.onclick (code.js:37)
Here is the new code:
If txtRegFullName = "" Then
NSB.MsgBox("Please enter a full name")
Else
cryptRegFullName = sjcl.encrypt(txtPassword, txtRegFullName)
NSB.MsgBox("Text is: " & txtRegFullName & " - Encrypted text is: " & cryptRegFullName)
End If
The ID of the jqWidget input control is txtRegFullName and the NAME property of the control is RegFullName. Should those be the same? What am I doing wrong? I always name my input controls with txt and then what the input is for in camel case (ie, txtFullName). I am not sure what the NAME property is in the control versus the ID (which I know is for css and html - to reference it).
I am using jqWidgets for the input and masked input (password) fields. I guess that’s the overall framework I am using. The rest are labels, containers and flexboxes to align the controls. This is for a desktop .exe app and not for mobile.
This is all very confusing. Here is my corrected code that does not work at all:
Function btnRegGo_onclick()
Dim txtRegFullName, txtRegAddress, txtRegPhone, txtRegEmail, txtRegUsername, txtRegPassword, txtPassword, webURL, newPublicKey, cryptRegFullName, cryptRegAddress, cryptRegPhone, cryptRegEmail, cryptRegUsername, cryptRegPassword
txtPassword = "98dgTGY341KJxcw"
If txtRegFullName.value <> "" Then
cryptRegFullName = sjcl.encrypt(txtPassword, txtRegFullName.value)
NSB.MsgBox("Text is: " & txtRegFullName.value & " - Encrypted text is: " & cryptRegFullName)
Else
NSB.MsgBox("Please enter a full name")
End If
End Function
I get no message box on error (if I don’t enter anything) and I get a error in the crypto.js file in the browser console if I do enter a name (or anything really). I get the following:
Uncaught TypeError: Cannot read property ‘value’ of undefined.
line 34 column 21 (in code.js)
If the error message is pointing to that line, it means that txtRegFullName.value is undefined.
That makes sense, since you have included it in the Dim statement a couple lines above. That will initialize a variable with that name in the function.
I’ll bet your txtRegFullName.value you want to get at is defined outside of the function already.
That was it. I remmed out (deleted) the Dim statement and viola - everything works.
WHY did that cause everything to mess up, though? Is it like option explicit in visual basic?
Obviously it’s a javascript thing. I don’t remember issuing any dim or var statements anywhere else in the program??? Javascript really confuses me sometimes.
So declare them outside the functions then? Removing the dim statement fixed things, but should I re-declare everything or just leave it all alone now?
Variables declared inside a function are only visible inside that function. They loose their content when you exit the function.
Code declared inside an if/then/else, for, do etc statement are only visible inside those statements. Once execution moves beyond those statements, those variables and their contents are gone.
So, think about what you’re doing inside your if/then/else and how long + when you need to access the data.