验证字段时,自动完成关闭工作异常

时间:2016-11-16 16:48:02

标签: javascript email-validation autofill

当我使用autocomplete="off"时,确认电子邮件验证会清除字段值,否则可以正常工作。当我登陆此页面以从任何其他页面编辑表单时,会发生这种情况。如果我尝试在确认电子邮件字段中键入新的电子邮件地址,则代码可以正常工作。自动填充数据是浏览器缓存的数据。

这是我的代码:

的Javascript

function validateEmails(){
if( document.getElementById('email').value != document.getElementById('confemail').value) {
    document.getElementById('confemail').setCustomValidity('Confirm Email ID and Email ID do not match');
  } else {
      document.getElementById('confemail').setCustomValidity('');
  }
}

document.getElementById('confemail').onkeyup = validateEmails;

HMTL

<div class="col-xs-6">
    <label class="control-label" for="confirmemail">Confirm
        Email address<span id="star"> *</span>
    </label>

    <form:input path="confirmemail" maxlength="50"
        class="form-control input-sm" type="email" id="confemail"
        placeholder="Please enter your Email Id" onfocus='this.autocomplete=\"off\";'/>

    <form:errors path="confirmemail" element="div" id="confirmemail"
        cssClass="alert alert-error" />
</div>

0 个答案:

没有答案