删除空格后的错误:JavaScript

时间:2013-11-14 03:14:38

标签: javascript web-services validation

我有一个用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;
                     }
           }

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

2 个答案:

答案 0 :(得分: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;       

答案 1 :(得分: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;
}