我有问题,如果我动态添加电子邮件输入而不是验证后,如果我在第一个输入中输入一些值而不是在secound电子邮件字段中,我会收到很多警报
function isEmail(email) {
emailValue=email.value;
var regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i;
var res = regex.test(emailValue);
if (res==false){
alert("Enter valid email");
}
}
动态创建电子邮件字段的代码 -
container.appendChild(document.createTextNode("email"));
var input_e = document.createElement("input");
input_e.type = "email";
input_e.id= id+'_email';
input_e.name= "email[]";
input_e.required =true;
input_e.setAttribute('onblur', 'return isEmail(email)');
container.appendChild(input_e);
container.appendChild(document.createElement("br"));
container.appendChild(document.createElement("br"));
HTML code:
Email :
<input type="email" id="email" name="email[]" placeholder="Enter email" onblur="return isEmail(email)" required>
<input type="button" id="newAutocomplete" onclick="addFields();" value="add" />
<br><br>
<div id="container" />
答案 0 :(得分:1)
如果我找到你的财产,你必须使用 -
function isEmail(email) {
emailValue=email.value;
var regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i;
var res = regex.test(emailValue);
if (res==false && !window.alertedOnce){
window.alertedOnce = true; // Use like a flag and set-rest as per your requirement
alert("Enter valid email");
}
}