0

我想在特定字符后验证 2 个字段:

<input id="email" class="txt" type="text" maxlength="255" value="" size="25" name="email"></input>
<input onblur="alexa()" id="domain" class="txt" maxlength="255" value="" size="25" name="domain" type="text">

我知道要验证它们,您会这样做

function BothFieldsIdenticalCaseSensitive2() {
 var two = document.getElementById('email').value;
 var three = document.getElementById('domain').value;

我知道将它们简单地从一个字段中的一个值与另一个值进行比较就像

 if(two == three) { return true; }
 alert("Warning!! passcodes must match!!!");
 return false;
}

但我需要知道:

// 你如何要求它比较字符“@”后面的两个字段,如果字符后面的字符匹配,则返回 true;else { alert (请添加网站电子邮件地址以便注册 (webmaster@domain.com)}

4

2 回答 2

1
function BothFieldsIdenticalCaseSensitive2() {
    var two = document.getElementById('email').value;
    var three = document.getElementById('domain').value;

    var twoIndexOfAt = two.indexOf("@");
    var threeIndexOfAt = two.indexOf("@");
    var match = twoIndexOfAt !== -1 && 
        threeIndexOfAt !== -1 &&
        two.substring(twoIndexOfAt + 1) === three.substring(threeIndexOfAt + 1);

    if (match) { 
        return true; 
    }
    else {
        alert("Warning!! passcodes must match!!!");
        return false;
    }
}
于 2013-10-02T14:01:12.053 回答
0

您可以在 char 上使用splitfunc @

var two = document.getElementById('email').value;
var three = document.getElementById('domain').value;

var first = two.split("@")[1]; //[1] to get the part after the split char
var second = three.split("@")[1];

//Compare
if (first == second)
    console.log("They match!");
于 2013-10-02T13:59:47.817 回答