Bagaimana cara melakukan Pernyataan JIKA ini sekaligus? Saya ingin memeriksa semua JIKA sebelum RETURN dan menampilkan semua pesan di document.getElementById('td100').innerHTML.

Misalnya: jika saya mengisi formulir saya dengan:

  1. email yang tidak valid (yaitu tanpa tanda @)
  2. email saya tidak cocok
  3. kata sandi saya tidak cocok
  4. dan kata sandi saya tidak memenuhi persyaratan panjang

Bagaimana cara menampilkan SEMUA kesalahan dalam (td100)?

function regValidation()
{
    document.getElementById('td100').innerHTML = "";
    event.preventDefault ? event.preventDefault() : event.returnValue = false; 

    var email1 = document.getElementById('em100').value;
    var email2 = document.getElementById('em101').value;
    var pass1 = document.getElementById('pw100').value;
    var pass2 = document.getElementById('pw101').value;

    if (!document.getElementById('em100').checkValidity()) return document.getElementById('td100').innerHTML += "</br>Emails are not valid.";
    if (email1 !== email2) return document.getElementById('td100').innerHTML += "</br>Emails do not match.";
    if (pass1 !== pass2) return document.getElementById('td100').innerHTML += "</br>Passwords do not match.";
    if (pass1.length < @minPass || pass1.length > @maxPass) return document.getElementById('td100').innerHTML += "</br>Password must be @minPass - @maxPass characters.";

    return true && regSuccess();
}
0
Doug Fresh 8 Agustus 2017, 21:58

2 jawaban

Jawaban Terbaik
    function regValidation()
{
    document.getElementById('td100').innerHTML = "";
    event.preventDefault ? event.preventDefault() : event.returnValue = false; 

    var error = "";
    var email1 = document.getElementById('em100').value;
    var email2 = document.getElementById('em101').value;
    var pass1 = document.getElementById('pw100').value;
    var pass2 = document.getElementById('pw101').value;

    if (!document.getElementById('em100').checkValidity()) error += "</br>Emails are not valid.";
    if (email1 !== email2) error += "</br>Emails do not match.";
    if (pass1 !== pass2) error += "</br>Passwords do not match.";
    if (pass1.length < @minPass || pass1.length > @maxPass) error += "</br>Password must be @minPass - @maxPass characters.";

    if (error !== "")
    {
        document.getElementById('td100').innerHTML = error
    } else {
    return true && regSuccess();
    }
}
0
Doug Fresh 8 Agustus 2017, 19:08

Ok, sekarang solusi yang benar-benar kering. Siapkan tabel dan ulangi di atasnya:

var checks = [
 [pass1===pass2,"password missmatch"],
 [email1===email2,"email missmatch"]
 //..
];

var passed = true;
var errors=[];
for(let [check,message] of checks){
  if(!check){
    alert(message)//show message
    //or collect:
    errors.push(message);

     passed = false;
   }
}

 //now passed contains the result:
if(passed)
  alert("wohoo, all fine ;)");

 //and errors can be shown:
 document.getElementById("err")
   .innerHTML=errors.join("<br>");
0
Jonas Wilms 8 Agustus 2017, 19:28