locked
After I added if\else to this code it stop working RRS feed

  • Question

  • User-2071692902 posted

    Hi,

    The following code was working fine.But after I added an if\else statement to it , it stop working.

    How to fix this error?

          <script>
              $(function () {
                 
    var userName = document.getElementById("myUserName").value;
               var regex = new RegExp("^[A-Za-z0-9]+$");
               if (regex.test(userName)) {
                  $("div.dropdown-menu a").click(function () {
    $('#conDivID').append("<span style='color: red'>" + userName + ': </span>');
    $('#conDivID').append('</br>');
    $("#conDivID").append($(this).find("img").clone(true));
    $('#conDivID').append('</br>');
    $('#conDivID').append("<span class='timeSpan'>" + timeStamp + '</span>');
    $('#conDivID').append('</br>');
                             }else {
    
    alert("Please Enter Your Username To Start Chat,Only English Letters
    And Numbers Allowed Without Spaces Between Them In.") } //To use scrolls on the conDivID. document.getElementById("conDivID").scrollTop = document.getElementById("conDivID").scrollHeight; }); }); }; </script>

    Tuesday, February 5, 2019 8:42 AM

Answers

  • User-893317190 posted

    Hi Omanxp45-1,

    You should also check the usename in your dropdown's  click event.

    $("div.dropdown-menu a").click(function () {

    var regex = new RegExp("^[A-Za-z0-9]+$");
    if (regex.test(userName)) { 
    $('#conDivID').append("<span style='color: red'>" + userName + ': </span>');
    $('#conDivID').append('</br>'); $("#conDivID").append($(this).find("img").clone(true));
    $('#conDivID').append('</br>'); $('#conDivID').append("<span class='timeSpan'>" + timeStamp + '</span>');
    $('#conDivID').append('</br>');
    }else{
    alert(`Please Enter Your Username To Start Chat,Only English Letters And Numbers Allowed Without Spaces Between Them In.`);
    } });

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 11, 2019 1:28 AM

All replies

  • User753101303 posted

    Hi,

    Missing or misplaced }. You are doing

    if (condition) { { } else // The quick trick is to scan { but your editor should do that as well. Using a proper code layout can also help.

    About debugging you should not start right away by reading your code. See https://www.w3schools.com/js/js_debugging.asp

    If using F12 Console your browser should tell you that you have a syntax error.

    Tuesday, February 5, 2019 12:52 PM
  • User-474980206 posted
    If you don’t set the user name to a value in the server code, then it will be blank and the click handler will not be set up.
    Tuesday, February 5, 2019 3:11 PM
  • User-893317190 posted

    Hi Omanxp45-1,

    Your js syntax format causes your problem. I don't know the logic of your js code, so I change your code so that it is executable, maybe its not your logic.

    Below is my code.

    <body>
           <input type="text" id="myUserName" />
        <div id="conDivID" style="width:70px;height:90px;border:1px solid red"></div>
    </body>
    
    
    <script>
              $(function () {
    
    var userName = document.getElementById("myUserName").value;
               var regex = new RegExp("^[A-Za-z0-9]+$");
               if (regex.test(userName)) {
                   $("div.dropdown-menu a").click(function () {
                       $('#conDivID').append("<span style='color: red'>" + userName + ': </span>');
                       $('#conDivID').append('</br>');
                       $("#conDivID").append($(this).find("img").clone(true));
                       $('#conDivID').append('</br>');
                       $('#conDivID').append("<span class='timeSpan'>" + timeStamp + '</span>');
                       $('#conDivID').append('</br>');
                   }) // add this line to end the click event binding
                   }else {
                     // if your string is beyond one line , please use Back quote ` to wrap your string instead of '
                        alert(`Please Enter Your Username To Start Chat,Only English Letters
                  And Numbers Allowed Without Spaces Between Them In.`);
               }
                      //To use scrolls on the conDivID.
                  document.getElementById("conDivID").scrollTop = 1234;
               document.getElementById("conDivID").scrollHeight;
                  });
             // });
    //};
    </script>

    If this is not your logic, please use F12  developer tool to see your error message.

    https://developers.google.com/web/tools/chrome-devtools/javascript/

    Best regards,

    Ackerly Xu
     

    Wednesday, February 6, 2019 5:42 AM
  • User-2071692902 posted

    Hi Ackerly Xu,

    I can't fixed it. Here is the whole page with the code inside it...

    http://redapple-chat.com/RAChat.html

    Wednesday, February 6, 2019 11:23 AM
  • User-893317190 posted

    Hi Omanxp45-1,

    What's your problem?

    I have checked your page, but the console doesn't show any error?

    Could you specify your problem?

    Best regards,

    Ackerly Xu

    Thursday, February 7, 2019 1:12 AM
  • User-2071692902 posted

    <br>
    Hi,&lt;br&gt;<br>
    The problem is when I send stickers from the dropdown menu the code must check the username that must be<br>
    Only in English Letters And Numbers Allowed Without Spaces Between Them In. And must not be blank.
    <br>
    Thursday, February 7, 2019 10:13 AM
  • User-893317190 posted

    Hi Omanxp45-1,

    You should also check the usename in your dropdown's  click event.

    $("div.dropdown-menu a").click(function () {

    var regex = new RegExp("^[A-Za-z0-9]+$");
    if (regex.test(userName)) { 
    $('#conDivID').append("<span style='color: red'>" + userName + ': </span>');
    $('#conDivID').append('</br>'); $("#conDivID").append($(this).find("img").clone(true));
    $('#conDivID').append('</br>'); $('#conDivID').append("<span class='timeSpan'>" + timeStamp + '</span>');
    $('#conDivID').append('</br>');
    }else{
    alert(`Please Enter Your Username To Start Chat,Only English Letters And Numbers Allowed Without Spaces Between Them In.`);
    } });

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 11, 2019 1:28 AM