Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 2 of 2
  1. #1
    Senior Member Ronald Roe's Avatar
    Join Date
    Mar 2011
    Location
    Oklahoma City
    Posts
    3,141
    Member #
    27197
    Liked
    959 times

    Need Another Set of Eyes - Conditional Always Goes to First Condition

    OK, here's the function:

    HTML Code:
    function dob_check(dob) {
        dob = dob.replace('-', '/');
        
       var parts = dob.split('/');
        var error = false;
            if (parts.length != 3) {
               error = true;
          } else {
               if (parts[0].match('[^0-9]')){
                   return false;
               }
               if (parts[1].match('[^0-9]')){
                   return false;
               }
               if (parts[2].match('[^0-9]')){
                   return false;
               }
    
               if (parts[0] < 1 || parts[0] > 12){
                   error = true;
               }
               if (parts[1] < 1 || parts[1] > 31){
                   error = true;
               }
               if (parts[2].length < 4){
                   error = true;
               }
           }
    
            console.log(error);
            return error;
    }
    Here's how it's used:
    HTML Code:
    window.onload = function(){
        $('input[name=dob]').on('change', function(){
            var input_dob = dob_check($(this).val());
            console.log(input_dob);
            if(input_dob === false){
                $(this).parent('td').parent('tr').children('td').children('button').removeAttr('disabled');
                $(this).siblings('div.error').hide().text('');
            } else {
                $(this).parent('td').parent('tr').children('td').children('button').attr('disabled', 'disabled');
                $(this).siblings('div.error').show().text('Date of birth must be in the format: "mm/dd/yyyy"');
            }
        });
    
    }
    Pardon the awful jQuery. I hate using it outside of AJAX, but I'm being somewhat forced here.

    As you can see, this is tied to the change event on an input. When logging the value, the correct value is pulled. The function itself returns properly as designed. However, for some reason, that first condition is always taken. It doesn't matter if I change the value so the function evaluates differently. Oddly enough, if I swap the layout of the conditional, it will still always run those 2 lines (the removeAttr and the empty .text()). I've been beating my head against this one for a couple hours, and I'm just not seeing the problem. No change affects the way it operates. Even switching false to true has no effect. Anything I log outputs the correct value. That's the part that really doesn't make sense. I could see if I was getting an unexpected value, but I'm not. No matter what, it always removes the attribute and hides the element.

    Even more curious, the following snippet works without issue:
    HTML Code:
         $('input[type=text]').on('change', function(){
            var input_text = $(this).val();
            if(input_text.length < 1){
                $(this).parent('td').parent('tr').children('td').children('button').attr('disabled', 'disabled');
                $(this).siblings('div.error').show().text('Field is required');
            } else {
                $(this).parent('td').parent('tr').children('td').children('button').removeAttr('disabled');
                $(this).siblings('div.error').hide().text('');
            }    
    });
    halp-my-brain-splode.jpg
    Last edited by Ronald Roe; Jan 14th, 2016 at 08:09 PM.
    Ron Roe
    Web Developer
    "If every app were designed using the same design template, oh wait...Bootstrap."

  2.  

  3. #2
    Senior Member Ronald Roe's Avatar
    Join Date
    Mar 2011
    Location
    Oklahoma City
    Posts
    3,141
    Member #
    27197
    Liked
    959 times
    Ok. Figured it out. It's an eye roller. So, the final snippet I posted above was running after the "problem" snippet. It was detecting that the input had text in it, and as you can see, it's the same 2 lines in both snippets.
    Ron Roe
    Web Developer
    "If every app were designed using the same design template, oh wait...Bootstrap."


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 08:45 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com