0

Simple Question really. I have a JS function that accepts a passed parameter. On the chance that the passed value is NULL I want trap for it but my limited JS experience is causing me to not have the proper syntax. Here it is. What do I need to do to change the syntax? There is more to the function obviously but the rest of it works fine when I don't trap for the NULL. The bad syntax is on the last line and I know it has to do with using a reserved word but I am not sure how to make it work.

<script type="text/javascript">

    //Pass UserName from text box on form to Ajax call 
  function CheckUserName(UserName_element) {

      var UserName = try { UserName_element.value; } catch (e) { };

For those of you asking: It is a js function inside of a vbscript Sub (unfortunately the whole site is written in classic asp)

    Sub UserNameValidation_Entry()
%>
<div id="username_validation" style="width:15px;position:relative;float:right;">
<img id="valid_UserName_image" src="<%=UrlForAsset("/images/tick.png")%>" border="0" style="display:none;" alt="User Name is Available." 
    title="User Name is Avaliable." />
<img id="invalid_UserName_image" src="<%=UrlForAsset("/images/icon_delete_x_sm.png")%>" border="0" style="display:none;" alt="User Name Already Exists." 
    title="User Name Already Exists." />
</div>

<script type="text/javascript">

        //Pass UserName from text box on form to Ajax call 
      function CheckUserName(UserName_element) {

          var UserName = UserName_element.value; 
          var UserName = try { UserName_element.value; } catch (e) { };


        $j.ajax({
            data: { action: 'CheckUserName', p_UserName: UserName },
            type: 'GET',
            url: 'page_logic/_check_username_ajax.asp',
            success: function (result) {

                //If user exists return X out, if not return green checkmark
                if (result == "True") {
                    try { valid_UserName_image.style.display = "none"; } catch (e) { };
                    try { invalid_UserName_image.style.display = "inline"; } catch (e) { };
                }
                else {
                    try { valid_UserName_image.style.display = "inline"; } catch (e) { };
                    try { invalid_UserName_image.style.display = "none"; } catch (e) { };
                    }
                }
            }
        );
        //return false;
    }
</script>

Called from here.

           <tr class="required_field">
            <td class="empty"></td>
            <td><b>Username:</b></td>
            <td class="label_value_separator"></td>
            <td>
                <input type='text' name="username" size="24" maxlength="50" value="<%=Session("s_username") %>" onblur="CheckUserName(this);">
                <% Call UserNameValidation_Entry() %>

        </tr>
Michael
  • 47
  • 6
  • 1
    What do you want your function to do if username is null? Also can you provide example of how you call `CheckUserName`? – jsalonen Feb 12 '15 at 21:33
  • 1
    When won’t you be sure whether the textbox exists? – Ry- Feb 12 '15 at 21:35
  • Learn how to use try/catch: http://www.javascriptkit.com/javatutors/trycatch.shtml -- such blocks do not return values. You can't say `var UserName = try...` as in your example, it's a syntax error. – Dexygen Feb 12 '15 at 21:45
  • Why do you wrap everything in try-catch blocks? They should be very rarely needed. – jsalonen Feb 12 '15 at 21:45
  • try catch is mostly used in javascript for native errors.. for example JSON.parse, or eval.. Is rare to throw intentionally errors. – rahpuser Feb 12 '15 at 21:48

1 Answers1

2

As minitech alludes to, you probably don't need to try/catch this scenario. That's only really an issue when attempting to access an object property that might be "undefined", or when you are trying to parse JSON that might be invalid, etc.

It would be sufficient simply to

if ( username !== null ) {
  // Send AJAX request
} else {
  // Send error to UI
}

There are many other JavaScript null-like detection patterns in this answer.

Community
  • 1
  • 1
msanford
  • 11,803
  • 11
  • 66
  • 93
  • 1
    Thanks everyone, the wrapping in try catch was a requirement from the boss, I was not originally using them. Could just be a standard here. I will go with the above suggestion. It seems simple enough. – Michael Feb 12 '15 at 21:56
  • If it's a requirement, wrap the above in a try? Bit lol-worthy but won't do anything. – msanford Feb 12 '15 at 23:59