首页 > 解决方案 > 如何使用 JavaScript 使我的网站显示输入不能为空?

问题描述

我编写了一个 javascript,如果用户的输入与我的 javascript 中的结果匹配,则将用户重定向到 URL,但我注意到即使用户将输入字段留空,用户仍将被重定向到 URL。当他们将输入字段留空时,如何发出“输入不能为空”的警报?

下面是我的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*/
  }
}

谢谢。

标签: javascript

解决方案


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

添加必填属性将不允许提交关联的空白字段。


推荐阅读