Javascript 表单验证问题

Javascript 表单验证问题,javascript,forms,validation,Javascript,Forms,Validation,我有一个很奇怪的问题。在表单中,我隐藏了值为-1的输入和用户名的输入字段 <form action="" method="POST" name="login" onSubmit="return Validate()"> <input type="text" id="username"/> <input type="hidden" id="available" value="-1"/> < input type="submit" value="S

我有一个很奇怪的问题。在表单中,我隐藏了值为-1的输入和用户名的输入字段

<form action="" method="POST" name="login" onSubmit="return Validate()">
  <input type="text" id="username"/>
  <input type="hidden" id="available" value="-1"/>
<  input type="submit" value="Send"/>
</form>
问题是Validate()仅在对一个条件求值时有效。If函数Validate()只包含1个变量(a或b)和1个If order(不包含else If),它工作正常。但当我这样说的时候,当Validate使用a和b变量,如果,否则,如果条件顺序,它将不起作用。真的。。先谢谢你

在这种情况下,它起作用:

function Validate(){

var a=document.getElementById("username").value;

if(a=="" || a==null)
{
    alert("Username cannot be empty");
    return false;
}
else
{
    return true;
}
}
<form action="" method="POST" name="login">
    <input type="text" id="username" />
    <input type="hidden" id="available" value="1" />
    <input type="button" value="Send" onClick="return Validate()" />
</form>
function Validate() {
    var a = document.getElementById("username").value;
    var b = Number(document.getElementById("available").value);
    if (a == "" || a == null) {
        alert("Username cannot be empty");
        return false;
    } else if (b < 0) {
        alert("Form isn't finished");
        return false;
    } else {
        document.login.submit();  //dynamically submit the form
    }
}

像这样试试 HTML:

function Validate(){

var a=document.getElementById("username").value;

if(a=="" || a==null)
{
    alert("Username cannot be empty");
    return false;
}
else
{
    return true;
}
}
<form action="" method="POST" name="login">
    <input type="text" id="username" />
    <input type="hidden" id="available" value="1" />
    <input type="button" value="Send" onClick="return Validate()" />
</form>
function Validate() {
    var a = document.getElementById("username").value;
    var b = Number(document.getElementById("available").value);
    if (a == "" || a == null) {
        alert("Username cannot be empty");
        return false;
    } else if (b < 0) {
        alert("Form isn't finished");
        return false;
    } else {
        document.login.submit();  //dynamically submit the form
    }
}

JS:

function Validate(){

var a=document.getElementById("username").value;

if(a=="" || a==null)
{
    alert("Username cannot be empty");
    return false;
}
else
{
    return true;
}
}
<form action="" method="POST" name="login">
    <input type="text" id="username" />
    <input type="hidden" id="available" value="1" />
    <input type="button" value="Send" onClick="return Validate()" />
</form>
function Validate() {
    var a = document.getElementById("username").value;
    var b = Number(document.getElementById("available").value);
    if (a == "" || a == null) {
        alert("Username cannot be empty");
        return false;
    } else if (b < 0) {
        alert("Form isn't finished");
        return false;
    } else {
        document.login.submit();  //dynamically submit the form
    }
}
函数验证(){
var a=document.getElementById(“用户名”).value;
var b=编号(document.getElementById(“可用”).value);
如果(a==“”| | a==null){
警报(“用户名不能为空”);
返回false;
}else if(b<0){
警告(“表单未完成”);
返回false;
}否则{
document.login.submit();//动态提交表单
}
}

如果您希望获得每个输入的错误通知,请不要在此处使用If/else,请使用多个If并设置错误

function validate(){
   var a=document.getElementById("username").value;
   var b=document.getElementById("available").value;
   var errors = [];

   if(a=="" || a==null){
      errors.push("Invalid username");
   }
   if(b<0 || isNaN(b)){
      errors.push("Invalid available value");
   }
   if(errors.length>0) {
      //do something with errors (like display them
      return false;
   }
}
函数验证(){
var a=document.getElementById(“用户名”).value;
var b=document.getElementById(“可用”).value;
var错误=[];
如果(a==“”| | a==null){
错误。推送(“无效用户名”);
}
if(b0){
//对错误进行处理(例如显示错误)
返回false;
}
}

如果其中一个计算结果为true,则使用else将跳过其他值。例如,如果第一个值为空或null,则它将执行该块并跳过其他值。

我正在测试IE和Firefox的代码,它可以工作。当您获得var b的值时,只需添加parseInt

var b= parseInt(document.getElementById("available").value);

看起来还可以。你说的不起作用是什么意思?它将数据发送到php文件,即使它不应该通过验证…非常奇怪…如果验证被写成两个函数,它对两种条件都有效,但在一个函数中它不起作用…是的,但是第一个条件呢,即使我将用户名输入留空id也不会像应该的那样提醒我…类型不是吗这里强制踢,“-1”小于0,因此
b@PatrickEvans是的,我认为这是边缘案例。但是我修改了代码以动态提交表单按钮被点击。