Link to home
Start Free TrialLog in
Avatar of Whing Dela Cruz
Whing Dela CruzFlag for Anguilla

asked on

Boolean

Hi experts, I wish to use Boolean to check whether true or false. The code below is not working, Any help please!

<!Doctype Html>
<Html>
<Body>
<input type="text" id="bday"/>
<button onclick="ValiDate()">Validate</button>

<script>
function ValiDate()
{
  if (ValiBday == true)
  {
    alert("true");
  }
  else
  {
    alert("false");
  }
}


function ValiBday()
{
//I get the value of the input element
  var inputValue=document.getElementById('bday').value;
// I create a new var with Regex rule to get only numbers
  var pattern =/^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/;
//I set a flag boolen to test if the inputValue has RegEx format
  var flag=pattern.test(inputValue);
//I create an if condition to check if flag is true or false
  if(flag===false){
    ValiBday = "false";
    return false;
  }  
  //I parse the value to date object time in unix format (ms)
  var birthday =Date.parse(document.getElementById('bday').value); 
  //I check the condition  
   if(isNaN(birthday))
    {
        ValiBday = "false";
        return false;
    } 
        ValiBday = "true";
}
</script>

</Body>
</Html>

Open in new window

SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Or if you want to tell the user:

   ...
  if (!isPattern || isNaN(birthday)) {
    alert("Please enter a valid date");
    return false; 
  }
  return true;// allow submit
}

Open in new window

Using a closure function inside the ValiDate function you can have the result you want. Remember (in a plain words) closure is a function that "dies" living behind a var, or boollean or any other primitive type..
function ValiDate(){
 
return (function()
{
  
//I get the value of the input element
  var inputValue=document.getElementById('bday').value;
// I create a new var with Regex rule to get only numbers
  var pattern =/^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/;
//I set a flag boolen to test if the inputValue has RegEx format
  var flag=pattern.test(inputValue);
//I create an if condition to check if flag is true or false
  if(flag===false){   
     return alert('False');
  }else{
     return alert('True');
  }  
})();

}

Open in new window

@Leonidas - that is horrendous code! Why would you ever want to go to such lengths?
Also OP is obviously not into closures yet, but even if they were, what is the point of such confusing code for such a simple issue?
@Michel The purpose of this forum is (i think) not only solve any problems but to creatιng the irritations to learn someone via various examples. So adding a different  approach solution I give the change to Whing to "dig" more deeper in his knowledge.
Avatar of Whing Dela Cruz

ASKER

Thank you guys for participating and providing me some examples and ideas, I have learned a lot from it. Could you provide me some example using Boolean to return true or false? Thank you!
My examples are all boolean

return isPattern && !isNaN(birthday);

Open in new window

If isPattern is true and isNaN is false, then the above will return true - e.g. birthday is ok

The second code tests the boolean value of the two and returns false if any of them are false
if (!isPattern || isNaN(birthday)) 

Open in new window

The code below may help me to solve my problem if anyone can correct it. The point here is I want the ValiBday to return as Boolean.  Due to some issue I need to achieve this if it is possible or may you experts can give me some ways to achieve with the same scenario. Thank you!

<!Doctype Html>
<Html>
<Body>
<input type="text" id="A1"/>
<button onclick="ValiDate()">Validate</button>


<script>
function ValiDate()
{
  if (ValiBday == true)
  {
    alert("true");
  }
  else
  {
    alert("false");
  }
}


function ValiBday()
{
  if (document.getElementById("A1").value == "A1")
    {
      ValiBday = true;
    }
  else
  {
     ValiBday = false; 
  }
  
}
</script>

</Body>
</Html>

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you so much Julian for your explanation, I really came from vb6 that's why my approach is something like  vb. More power to all of you and may God bless you as always!
The accepted answer is far too unnecessarily verbose

Whenever you have a boolean value, you can return it
function ValiDate() {
  if (ValiBday())  { // unnecessary test if it is == true which will convert a truthy value to boolean
    alert("true");
  }
  else
  {
    alert("false");
  }
}

function ValiBday() {
  return document.getElementById("A1").value === "A1") ; // the === is strictly true, so it is already a boolean
}

Open in new window