简体   繁体   English

我无法在用于表单验证的 java 脚本中获得“else if”部分代码

[英]I can't get “else if ” part of the code in java-script for form-validation

I am doing a login form ,where, if the input fields is empty I want to declare an error message.我正在做一个登录表单,如果输入字段为空,我想声明一条错误消息。 I am only getting the 'if' part of the code but not the 'else if' part.我只得到代码的“if”部分,而不是“else if”部分。

 var a = document.getElementById('fname'); var b = document.getElementById('pwd'); var c = document.getElementById('demo1'); var d = document.getElementById('demo2'); function func() { if (a.value == null || a.value == "") { c.innerHTML = 'Fields can\\'t be empty'; a.style.border = "2px solid red"; return false; } else if (b.value == null || b.value == "") { d.innerHTML = "Password should be minimum 6 character"; b.style.border = "2px solid red"; return false; } else { true } }
 input { display: block; }
 <form action="" onsubmit="return func()"> <input type="text" name="fname" id="fname" placeholder="first name"><br><br> <p id="demo1" style="color: red;"></p> <input type="password" name="pwd" id='pwd' placeholder='password'><br><br> <p id="demo2" style="color: red;"></p> <button type="submit" id="smbt"> sumbit</button> </form>

Please try the below code hope it helps.请尝试下面的代码希望它有帮助。

function func() {
    var IsValid = true;
    if (a.value == null || a.value == "") {
        c.innerHTML = 'Fields can\'t be empty';
        a.style.border = "2px solid red";
        IsValid = false;
    }
    if (b.value == null || b.value == "") {
        d.innerHTML = "Password should be minimum 6 character";
        b.style.border = "2px solid red";
        IsValid = false;
    }
    return IsValid;
}
var a = document.getElementById('fname');
var b = document.getElementById('pwd');
var c = document.getElementById('demo1');
var d = document.getElementById('demo2');

function func() {

  if (a.value == null || a.value == "") {
    c.innerHTML = 'Fields can\'t be empty';
    a.style.border = "2px solid red";
   }
  if (b.value == null || b.value == "") {
    d.innerHTML = "Password should be minimum 6 character";
    b.style.border = "2px solid red";
   }



  if (a.value == null || a.value == "") {
    return false;
  } else if (b.value == null || b.value == "") {
    return false;
  } else {
    true
  }

}

HTML : HTML :

<form action="" onsubmit="return func()">
  <input type="text" name="fname" id="fname" placeholder="first name"><br><br>
  <p id="demo1" style="color: red;"></p>
  <input type="password" name="pwd" id='pwd' minlength="6" placeholder='password'><br><br>
  <p id="demo2" style="color: red;"></p>
  <button type="submit" id="smbt"> sumbit</button>
</form>

Use something like that - it will dislay both messages if you leave empety login and password.使用类似的东西 - 如果您留下空的登录名和密码,它将显示两条消息。 And HTML include 6 chars in your password.并且 HTML 在您的密码中包含 6 个字符。

You need to use minlength="6" in your input attribute just.您只需要在输入属性中使用 minlength="6" 即可。

Also quoting @vaibhav 's ans.还引用了@vaibhav 的回答。 I would add extra functionality by setting the min lenght of the password to 6. Also set the required field in input tag我会通过将密码的最小长度设置为 6 来添加额外的功能。同时在input标签中设置required字段

 var a = document.getElementById('fname'); var b = document.getElementById('pwd'); var c = document.getElementById('demo1'); var d = document.getElementById('demo2'); function func() { var IsValid = true; /* // remove this line if u dont want the required attribute in input tag i added c.innerHTML =""; // if you got the name empty the first time and a.style.border = "2px solid green"; //then corrected the name and got the passwprd wrong, */ d.innerHTML =""; // both the name and the pass would be in red. b.style.border = "2px solid green"; // Also the reason for setting the `innerHTML =""` /* // remove this line too if u dont want the required attribute in input tag i added if (a.value == null || a.value == "") { c.innerHTML = 'Fields can\\'t be empty'; a.style.border = "2px solid red"; IsValid = false; } // */ if (b.value.length < 6) { // min lenght of password d.innerHTML = "Password should be minimum 6 character"; b.style.border = "2px solid red"; IsValid = false; } return IsValid; }
 input { display: block; }
 <form action="" onsubmit="return func()"> <input type="text" name="fname" id="fname" placeholder="first name" required><br><br> <p id="demo1" style="color: red;"></p> <input type="password" name="pwd" id='pwd' placeholder='password' required><br><br> <p id="demo2" style="color: red;"></p> <button type="submit" id="smbt"> sumbit</button> </form> <!-- with the required attribute in input tag a user would not be able to keep them empty in this case-->

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

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