Results 1 to 2 of 2

Thread: Superfluous Code?

  1. #1
    Join Date
    Apr 2012
    Location
    Central New Jersey
    Posts
    286
    Thanks
    95
    Thanked 3 Times in 3 Posts

    Default Superfluous Code?

    I am looking at a script that has this code:

    Code:
    	 if ( form.yourName.value.length == 0 ) {
    		 alert( "Please provide your name." );
    		 form.yourName.focus();
    		 return false;
    Then, later on, it has this code:

    Code:
      var findSpace = /^(.+) (.+)$/;
      var matchFlag = form.yourName.value.match( findSpace );
      if ( matchFlag == null ) {
        alert( "Please provide both a first and last name, and resubmit." );
        form.yourName.focus();
        return false;
      }
    My impression is that the first test is superfluous because the second test automatically encompasses the first. I would appreciate someone telling me whether I am missing something.

    Thanks, as always.

    A.

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Yeah, though I would drop the second test rather than the first.

    Reason being:

    It's unreliable. There's nothing to guarantee that "something" + whitespace + "something" is "firstname lastname". It could be "# :)".

    It's inefficient. Why would you care about first and last names unless you're going to store them as such? Therefore, you're going to have to separate the first and last when you process the form. What if the user gets confused and enters "Smith, John"? What if the user enters three words? If you need first + last, it's a better approach to ask for them in separate inputs.

    One might argue that, since the user can read the form label that says "Your Name" (or whatever), it's reasonable to assume that they do indeed write their name in the text box. But, by that logic, why validate it at all? (I'm being completely serious.) Don't you trust that the user knows what their own name is? Just make sure they didn't forget to fill it in.

    ------------
    On the other hand, the obvious reason to have two different tests is to be able to provide more specific error messages, which is a Good Thing.
    But that can be handled more reliably by using separate inputs.

  3. The Following User Says Thank You to traq For This Useful Post:

    marain (02-13-2013)

Similar Threads

  1. Replies: 3
    Last Post: 05-12-2011, 03:43 AM
  2. Secret code / hidden message / obfuscated code - ???
    By newbie01.others in forum JavaScript
    Replies: 2
    Last Post: 01-14-2011, 11:29 AM
  3. Replies: 15
    Last Post: 06-11-2009, 12:27 PM
  4. Replies: 2
    Last Post: 10-27-2008, 05:16 AM
  5. Help - Code discrepency - Full Screen code
    By kurson in forum Dynamic Drive scripts help
    Replies: 17
    Last Post: 05-23-2006, 05:21 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •