【问题标题】:code is excecuted even when the condition is not satisfied即使条件不满足也会执行代码
【发布时间】:2020-02-26 14:23:38
【问题描述】:

我编写了一个代码来验证唯一 ID 至少为 4 个字符、至少 1 个小写字符和至少 1 个数字

这是css代码

<style>

.valid {
  color: green;
}
.invalid {
  color: red;
}
.button {
  display: inline-block;
  padding: 15px 25px;
  font-size: 24px;
  cursor: pointer;
  text-align: center;
  text-decoration: none;
  outline: none;
  color: #fff;
  background-color: #4CAF50;
  border: none;
  border-radius: 15px;
  box-shadow: 0 9px #999;
}

.button:hover {background-color: #3e8e41}

.button:active {
  background-color: #3e8e41;
  box-shadow: 0 5px #666;
  transform: translateY(4px);
}
#grad {
  background-image: linear-gradient(to top right, #33ccff 10%, #ff99cc 100%);
}
</style>

输入代码

<input type="button" class="button"  value="Save" onclick="save_user();" autocomplete="on" />

这是脚本代码

<script>

  var tblUsers = document.getElementById('tbl_users_list');
  var databaseRef = firebase.database().ref('users/');
  var rowIndex = 1;
  var uid;
  var childKey;
  var childData;

  databaseRef.once('value', function(snapshot) {
    snapshot.forEach(function(childSnapshot) {
    childSnapshot.key;
    childSnapshot.val();

   var row = tblUsers.insertRow(rowIndex);
   var cellId = row.insertCell(0);
   var cellName = row.insertCell(1);
   cellId.appendChild(document.createTextNode(childKey));
   cellName.appendChild(document.createTextNode(childData.user_name));
   cellName.appendChild(document.createTextNode(childData.user_name2));

   rowIndex = rowIndex + 1;


    });
  });



function check()
{


 var myInput = document.getElementById("user_name2");
  var letter = document.getElementById("letter");

  var number = document.getElementById("number");
  var length = document.getElementById("length");

  // When the user clicks on the password field, show the message box
  myInput.onfocus = function() {
    document.getElementById("message").style.display = "block";
  }

  // When the user clicks outside of the password field, hide the message box
  myInput.onblur = function() {
    document.getElementById("message").style.display = "none";
  }

  // When the user starts to type something inside the password field
  myInput.onkeyup = function() {
    // Validate lowercase letters
    var lowerCaseLetters = /[a-z]/g;
    if(myInput.value.match(lowerCaseLetters)) {  
      letter.classList.remove("invalid");
      letter.classList.add("valid");
    } else {
      letter.classList.remove("valid");
      letter.classList.add("invalid");
    }



    // Validate numbers
    var numbers = /[0-9]/g;
    if(myInput.value.match(numbers)) {  
      number.classList.remove("invalid");
      number.classList.add("valid");
    } else {
      number.classList.remove("valid");
      number.classList.add("invalid");
    }

    // Validate length
    if(myInput.value.length >= 4) {
      length.classList.remove("invalid");
      length.classList.add("valid");
    } else {
      length.classList.remove("valid");
      length.classList.add("invalid");
    }
  }




return true;



}










if(check())
{

  function save_user(){

var user_name = document.getElementById('user_name').value;
   var user_name2 = document.getElementById('user_name2').value;

   uid = firebase.database().ref().child('users').push().key;

   var data = {
    user_id: uid,
    user_name: user_name,
    password: user_name2
   }

   var updates = {};
   updates['/users/' + uid] = data;
   firebase.database().ref().update(updates);

   alert('The user is created successfully!');
   reload_page();

   document.writeln(uid);


  }
}
else{
  alert('condition not satisfied');
}
  function update_user(){
   var user_name = document.getElementById('user_name').value;
   var user_id = document.getElementById('user_id').value;

   var data = {
    user_id: user_id,
    user_name: user_name
   }

   var updates = {};
   updates['/users/' + user_id] = data;
   firebase.database().ref().update(updates);

   alert('The user is updated successfully!');

   reload_page();
  }

验证过程在函数check()中

函数返回true

在主脚本文件中,它应该只在函数 check() 为真时执行,但即使它不为真也会执行

我想要从这段代码中只在函数 check() 为真时才将值上传到数据库中 我应该对此代码进行哪些更改?

【问题讨论】:

  • 你在check()函数的末尾有return true,所以它总是会进入if(check())并保存用户
  • 而且我也面临像 save_user not a function 之类的错误,一旦我打开网页,else alert('不满意')就会被执行,请帮助

标签: javascript html firebase firebase-realtime-database


【解决方案1】:

你最后有return true。所以它返回一个真值。所以当if(check()) 被选中时,它会得到一个真值。更简单的代码可以实现相同的输出。

当您单击提交按钮时,不会调用您的 save_user() 函数。它在if 条件内。

【讨论】:

  • 我也面临像 save_user not a function 这样的错误,一旦我打开网页,else alert('not satisfied') 就会被执行,请帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-07
  • 1970-01-01
  • 2020-12-24
  • 2015-07-12
  • 1970-01-01
  • 1970-01-01
  • 2012-10-01
相关资源
最近更新 更多