-1

我有一个用 JavaScript 编码的网站。但是,每当用户尝试注册时,如果他们在确认电子邮件后留下了多余的空格,则会出现错误,指出电子邮件和确认电子邮件不匹配。我尝试使用 trim() 方法解决此问题,但现在我没有收到错误消息。但是,如果用户输入电子邮件,即使确认电子邮件不同,用户仍然可以注册。例如:jdoe@email.com 和 jdoe2@email.com 在不应该的情况下都被视为平等。这是我的代码:

if(isBlank(document.forms.mainForm.confirmEmail.value)) {
  alert("Please confirm the bill to E-mail address.");
  document.getElementById("confirmEmail").style.color = "red";
  blackText=document.getElementById("confirmEmail");
  document.forms.mainForm.confirmEmail.focus();
  return false;
 }

 else if(!isBlank(document.forms.mainForm.confirmEmail.value))
  {
      var checkConfirm = document.forms.mainForm.confirmEmail.value;
      var emailConfirmTrim = checkConfirm.trim();
      return emailConfirmTrim;
    }

    else{
       if(emailConfirmTrim != document.forms.mainForm.email.value) {
       alert("The confirm E-mail address does not match the E-mail address.");
       document.getElementById("confirmEmail").style.color = "red";
       blackText=document.getElementById("confirmEmail");
       document.forms.mainForm.confirmEmail.focus();
       return false;
                     }
           }

我只想从确认电子邮件中删除尾随空格,以便如果电子邮件和确认相同,则用户可以注册。我哪里做错了?提前致谢。

4

2 回答 2

1

我不认为你真的需要第二个 if 块,你没有做任何检查来查看确认电子邮件是否与电子邮件相同。您的代码永远不会进入第三个 if 块。你可以这样重写你的代码:

if(isBlank(document.forms.mainForm.confirmEmail.value)) {
  alert("Please confirm the bill to E-mail address.");
  document.getElementById("confirmEmail").style.color = "red";
  blackText=document.getElementById("confirmEmail");
  document.forms.mainForm.confirmEmail.focus();
  return false;
 }                
 //else if(!isBlank(document.forms.mainForm.confirmEmail.value))
 // {
 //     var checkConfirm = document.forms.mainForm.confirmEmail.value;
 //     var emailConfirmTrim = checkConfirm.trim();
 //     return emailConfirmTrim;
 //   }
 else if(document.forms.mainForm.confirmEmail.value.trim() != document.forms.mainForm.email.value) {
       alert("The confirm E-mail address does not match the E-mail address.");
       document.getElementById("confirmEmail").style.color = "red";
       blackText=document.getElementById("confirmEmail");
       document.forms.mainForm.confirmEmail.focus();
       return false;
                     }
 else
       return true;       
于 2013-11-14T03:26:40.917 回答
0

重构的代码,未经测试,但会给你一些开始。

function getEmail() {
  //Get the elements of the input fields
  var email1 = document.forms.mainForm.email;
  var email2 = document.forms.mainForm.confirmEmail;

  // Trim the values
  email1.value = email1.value.trim();
  email2.value = email2.value.trim();

  if (!email1.value) {
    alert("Please enter bill to E-mail address.");
    email1.focus();
    return false;
  }

  if (!email2.value) {
    alert("Please confirm the bill to E-mail address.");
    email2.style.color = "red";
    email2.focus();
    return false;
  }

  if (email1.value != email2.value) {
    alert("The confirm E-mail address does not match the E-mail address.");
    email2.focus();
    return false;
  }

  // Return the email adress
  return email1.value;
}
于 2013-11-14T03:31:22.757 回答