jQuery 基本表单验证如果或
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4745832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery basic form validation if or
提问by benhowdle89
this is not working and i cant really see anything wrong with it, looks straightforward enough:
这不起作用,我真的看不出它有什么问题,看起来很简单:
$("#formLogin").submit(function(){
var username = $("#username").val();
var password = $("#password").val();
if (username == ' ' || password == ' '){
return false;
}
else return true;
});
EDIT:
编辑:
Its still submitting the form even if the fields are empty
即使字段为空,它仍在提交表单
回答by Vincent Ramdhanie
Are you trying to detect blank usernames and passwords?
您是否正在尝试检测空白用户名和密码?
$("#formLogin").submit(function(){
var username = $("#username").val();
var password = $("#password").val();
if (jquery.trim(username) == '' || jquery.trim(password) == ''){
return false;
}
else return true;
});
jquery.trim()removes leading and trailing spaces, so just in case the user types a number of space characters it will be detected.
jquery.trim()删除前导和尾随空格,所以万一用户输入了一些空格字符,它将被检测到。
回答by Mayank
Instead of doing this, why not simply use the jQuery Validation Plugin - http://docs.jquery.com/Plugins/validation
而不是这样做,为什么不简单地使用 jQuery 验证插件 - http://docs.jquery.com/Plugins/validation
VALIDATE FORMS LIKE YOU'VE NEVER BEEN VALIDATING BEFORE! (According to the site)
以前所未有的方式验证表单!(据现场报道)
回答by Rob
In the html add this
<input type="submit" value="SomeButton" onclick="return submitClicked();" />
在html中添加这个
<input type="submit" value="SomeButton" onclick="return submitClicked();" />
in the js do this
在 js 中这样做
function submitClicked() {
if ($("#passwordInput").val() == '')
{
alert('missing password field');
return false;
}