javascript - 如何使用 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*/
}
}
谢谢。
解决方案
<input type="text" required value="" />
添加必填属性将不允许提交关联的空白字段。
推荐阅读
- sql - 在嵌套选择中使用别名时 SQL 查询引发错误
- java - 为什么这个“最大”操作没有找到流中的最高数字?
- mysql - 选择列值大于另一列值的行
- javascript - 如何使用 jQuery 按自定义列对表格进行排序
- javascript - 忽略 Javascript 中的连字符(邮递员)
- python-3.x - Python:如果任何条目为空,则复制其上方的条目
- javascript - `Object.defineProperty(exports, '__esModule', { value: true })` vs `exports.__esModule = true`
- asp.net-core-mvc - 在数据传输对象(dto)模型上使用 DataAnnonations 约束来更新域模型
- html - gulp-pug 可以使用哪些选项?
- mysql - 如何使用 if else 使用列选择 sql