Jump to content

Using Try...Catch versus nested IF statements


dshevnock

Recommended Posts

Is it better practice or more effecient to use nested TRY...CATCH statements as opposed to nested IF statements? The reason I ask is because I am trying to develop a signup form that allows a user to add his/her e-mail address (optional) with 2 textboxes in the form (email and verifiedEmail).

 

 

Link to comment
Share on other sites

I think it's more a matter of preference.  Try doing it one way and if you like how it looks and it works, great.  If it works but you don't like how it looks, try it the other way.

 

I imagine you're doing some sort of form validation prior to submittal to the server and you want to cancel it if the e-mail fields do not match.

 

Here is a simple example:

function formSubmit(){
  var valid = true;
  var errs = new Array();

  var eml1 = document.getElementById("eml1");
  var eml2 = document.getElementById("eml2");

  // Force user to enter an e-mail, you said it's optional, so this test doesn't really apply
  // Note that you should really trim appending / trailing white space as well
  if(eml1.value.length == 0 ||
     eml2.value.length == 0){
    valid = false;
    errs.push("You must enter an e-mail address.");
  }

  // E-mail fields have to be equal
  if(eml1.value != eml2.value){
    valid = false;
    errs.push("Your e-mail fields must match.");
  }

  // More validating...

  // Now we can check if the form is valid
  if(!valid){
    // Form is not valid, stop form submittal.
    // Optionally, we can take the text strings in our errs variable and create an
    // UL element and place it at the top of our form
  }
}

Link to comment
Share on other sites

I think it's more a matter of preference.  Try doing it one way and if you like how it looks and it works, great.  If it works but you don't like how it looks, try it the other way.

 

I felt more comfortable using the if...elseif statements, so that is what I went with.

 

I imagine you're doing some sort of form validation prior to submittal to the server and you want to cancel it if the e-mail fields do not match.

 

I am definitely doing some form validation with Ajax to make the user experience a little bit better. If there are any error messages, the error message that is associated with the field appears below the form field. (And I am trying to learn Ajax so I thought this would be a good guniue pig for my learning.)

 

I was going to have the user input the email address, then re-input it for verification sakes, but have since removed the second textbox to re-enter the password. Now that I am thinking about it though, I think I will end up putting the verification part back in. Here is the JavaScript that I came up with for just the e-mail entry field.

 

function checkEmail(){
  var objEmail = document.getElementById('email');
  var varEmail = objEmail.value;

  if(varEmail != ""){							
    var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;

    var returnval=emailfilter.test(varEmail);
    if (returnval==false){
      document.getElementById('errorInvalidEmail').style.display = 'block';
      objEmail.focus();
    }
    else{
      document.getElementById('errorInvalidEmail').style.display = 'none';
    }
    return returnval;
  }
  else{
     document.getElementById('errorInvalidEmail').style.display = 'none';
  }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.