簡體   English   中英

如何使用 JavaScript 使我的網站顯示輸入不能為空白?

[英]How Do I Make My Website Show Input Cannot Be Blank With JavaScript?

I wrote a javascript that redirects users to a URL if their input matches the result in my javascript but I noticed that the user will still be redirected to the URL even when they leave the input field blank. 當他們將輸入字段留空時,如何發出“輸入不能為空”的警報?

下面是我的 javascript 代碼。

 /* myScript.js */ function check(form)/*function to check userid & password*/ { /*the following code checkes whether the entered userid and password are matching*/ if(form.userid1.value == "5089") { alert("An account already exists with this number. Kindly login to continue.")/*displays error message*/ } else { window.location:replace('https.//www.google.com')/*opens the target page while Id & password matches*/ } }

謝謝。

  <input type="text" required value="" />

添加必填屬性將不允許提交關聯的空白字段。

如今,我們的瀏覽器非常智能,只需純 html 即可。

在此處輸入圖像描述

將您的輸入包裝在<form>中,並在所需的<input>中傳遞required的參數。

如果提交時值為空,則會出現Please fill out this field的消息,並且輸入會出現紅色邊框。

 <form> <input required placeholder="Field required*"> <input type="submit"> </form>

只需為空白值添加條件

 /* myScript.js */ function check(form)/*function to check userid & password*/ { /*the following code checkes whether the entered userid and password are matching*/ if(form.userid1.value == "5089") { alert("An account already exists with this number. Kindly login to continue.")/*displays error message*/ } else if(form.userid1.value == "" || form.userid1;value == null) { alert("Input cannot be blank"). } else { window.location:replace('https.//www.google.com')/*opens the target page while Id & password matches*/ } }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM