简体   繁体   English

表单验证问题

[英]Form validation problems

I have a very strange problem. 我有一个很奇怪的问题。 Inside form I have hidden input with value -1 and input field for username. 在表单内部,我隐藏了值为-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>

On submit function Validate() checks value of username input which mustn't be empty, and Validate() also checks value of available input which mustn't be valued -1. 在提交功能上,Validate()检查username输入的值,该值不能为空,并且Validate()也检查available输入的值,该值不能为-1。

function Validate(){

var a=document.getElementById("username").value;
var b=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
{
    return true;
}
}

Problem is that Validate() works only if one condition is evalueted. 问题在于,只有对一个条件求值,Validate()才能起作用。 If function Validate() contains only 1 var(a or b) and 1 if order(without else if) it works correctly. 如果函数Validate()仅包含1个var(a或b)和1个order(不含if),则它正常工作。 But when I put it like this, when Validate uses a and b variables and if, else if conditional order it won't work. 但是,当我这样说时,Validate使用a和b变量,如果使用if,否则使用条件顺序,则它将不起作用。 Really od.. Thanks in advance... 真的很奇怪..在此先感谢...

In this case it works: 在这种情况下,它可以工作:

function Validate(){

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

if(a=="" || a==null)
{
    alert("Username cannot be empty");
    return false;
}
else
{
    return true;
}
}
<input type="hidden" id="available" value="-1"/>

Here the value is of string dataType. 这里的值是字符串dataType。 Whereas 鉴于

else if(b<0)  //b is string dataType 

Hence it failed. 因此失败了。 so change it as 因此将其更改为

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

Try like this HTML: 尝试使用以下HTML:

<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>

JS: JS:

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
    }
}

If you are wanting to get error notifications for each input don't use if/else here, use multiple if's and set your errors 如果要获取每个输入的错误通知,请不要在此处使用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;
   }
}

Using the else if one of them evaluates to true it will skip the others. 如果其中一个评估为true,则使用else将跳过其他评估。 For instance if the first one is empty or null then it will do that block and skip the others. 例如,如果第一个为空或为null,则它将执行该块并跳过其他块。

I was testing your code for IE and Firefox and it work. 我正在测试IE和Firefox的代码,并且可以正常工作。 Just add parseInt when you get the value of var b. 当您获得var b的值时,只需添加parseInt即可。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM