Why is this code wrong and throwing errors?

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.

Not sure how to do this in NSBasic.

Mike

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

Thanks all.

Mike

And without the .value commands:

    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

Still not working.

Mike

And adding the else if command also did not work:

 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

Mike

You forgot the “Then”:

If txtRegFullName = "" Then

Eric

weird. I had that before I posted and took it out for some reason.

That fixes the throw error but the second part does not work and throws the error - after the else

If I leave the text input blank, no message box warning me and I get errors anyway.

Mike

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.

Mike

When getting (or setting) the value of an input control, always use .value:

txtRegFullName.value

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)

What am I doing wrong now?

Mike

This is the converted javascript straight from the browser:

btnRegGo.onclick = function() {
 var txtRegFullName,txtRegAddress,txtRegPhone,txtRegEmail,txtRegUsername,txtRegPassword,txtPassword,webURL,newPublicKey,cryptRegFullName,cryptRegAddress,cryptRegPhone,cryptRegEmail,cryptRegUsername,cryptRegPassword;

  txtPassword = "98dgTGY341KJxcw";

  if(txtRegFullName.value != "") {
    cryptRegFullName = sjcl.encrypt(txtPassword, txtRegFullName.value);
    NSB.MsgBox("Text is: "  +  txtRegFullName.value  +  " - Encrypted text is: "  +  cryptRegFullName);
 } else {
    NSB.MsgBox("Please enter a full name");
  }

};

It shows an error at the end of this line: if(txtRegFullName.value != “”) {

Not sure why it’s wrong? Should the double quotes be single quotes in javascript? This is really frustrating.

Mike

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.

Mike

If you declare a variable within function, it becomes local to that function. That is true in both BASIC and JavaScript.

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?

Mike

Scope:

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.

You didn’t actually explicitly “declare” txtRegFullName, I don’t believe. Isn’t it just one of the controls in your HTML form?

Further to @RustySabre, all the control names in your project are initialized automatically when they created.