在 JavaScript 中檢查數值是否為空


使用包含“”和 NULL 的條件來檢查數值是否為空。ua 使用者未填寫文字框值時,丟擲訊息。

示例

 即時演示

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initialscale=1.0">
<title>Document</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
<form name="register" onsubmit="return checkingUserName()">
USERNAME: <input type="text" name="username">
<input type="submit" value="Save">
</form>
<script>
   function checkingUserName() {
      var username = document.forms["register"]["username"].value;
      if (username == null || username == "") {
         alert("Please enter> the username. Can’t be blank or empty !!!");
         return false;
      }
   }
</script>
</body>
</html>

要執行上述程式,請儲存檔名為“anyName.html(index.html)”,然後右鍵單擊該檔案。在 VS Code 編輯器中選擇“用 Live 伺服器開啟”選項。

輸出

這將產生以下輸出 −

單擊“儲存”按鈕後,您將收到以下警報以及一條訊息 −

更新日期:01-Sep-2020

20K+ 次瀏覽

開啟您的職業生涯

透過完成課程獲得認證

立即開始
廣告