We help IT Professionals succeed at work.

Boolean

Whing Dela Cruz
on
232 Views
Last Modified: 2017-04-25
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

Comment
Watch Question

HainKurtSr. System Analyst
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009

Commented:
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

CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
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

Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009

Commented:
@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?
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
@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.

Author

Commented:
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!
Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009

Commented:
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

Author

Commented:
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

CERTIFIED EXPERT
Distinguished Expert 2017
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
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!
Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009

Commented:
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

Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.