0

I'm trying to compare two variables values, but it is not working as expected, I need to compare a static id (0000) four zero's with what has been entered by user(userInput), I tried to print values but it prints only first zero and skips rest, also it compare user value with only first zero, I need to compare user entered value with exact four zeros Any idea what is wrong?

function showProfile() {
 var checkEmpId= document.getElementById("emp_id").value;
    var staticEmpId=0000; 
    console.log(+checkEmpId); // from user Input
    console.log(+staticEmpId); // shows only one 0
    setTimeout(function(){
 //    I thought this will help ,still not working
        if(parseInt(checkEmpId,4)== parseInt(staticEmpId,4)){
        
// I tried below as if statement but no luck
//if((document.getElementById("emp_id").value)="0000"){
console.log("Yay, Got luck"); }
        else
        {
             console.log("else block is now clalled");
        errorProfileId();
        }
    },4000)
}
<label>INCR</label><input type="text" class="form-control" name="otp" id="emp_id" required size="4" maxlength="4">
  <button type="submit" class="btn btn-default btn-success" tabindex="-1" id="register_emp_btn"  dismiss="modal" onclick="showProfile();">Compare</button>

 funcion showProfile(){
 var checkEmpId= document.getElementById("emp_id").value;
    var staticEmpId=1234;
    console.log(+checkEmpId);
    console.log(+staticEmpId);
    setTimeout(function(){
        if(parseInt(checkEmpId,4)== parseInt(staticEmpId,4)){
        
//        if((document.getElementById("emp_id").value)="0000"){
           var checkID=document.getElementById("emp_id").value;
         localStorage.setItem("LSEmpId",document.getElementById("emp_id").value);
            window.location.replace("signup_step2.html");
          
        }
        else
        {
             console.log("else block is now clalled");
        ErrorProfile();
        }
    },4000)
}

Prasad_Joshi
  • 642
  • 3
  • 13
  • 34

2 Answers2

0

In javascript (i would even say most programming languages) leading 0s on integers are being ignored, so that is the reason you are getting only a single 0. A simple workaround would be to use a string as "0000" to do your comparison. Hope this helps!

N. Ivanov
  • 1,795
  • 2
  • 15
  • 28
0

function showProfile() {
 var checkEmpId= document.getElementById("emp_id").value;
    var staticEmpId="0000"; 
    document.write("<br>"+checkEmpId); // from user Input
    document.write("<br>"+staticEmpId); // shows only one 0
    setTimeout(function()
    {
        if(checkEmpId===staticEmpId){
            document.write("<br>Yay, Got luck"); 
        }else{
            document.write("<br>else block is now called");
        }
    },4000)
}
<label>INCR</label><input type="text" class="form-control" name="otp" id="emp_id" required size="4" maxlength="4">
  <button type="submit" class="btn btn-default btn-success" tabindex="-1" id="register_emp_btn"  dismiss="modal" onclick="showProfile();">Compare</button>