2015-04-25 47 views
-1

我正在檢查HTML輸入表單以查看它是否包含@符號。Javascript在電子郵件中檢查@

HTML:

<input id="emailCheck" type="text" name="uid" /> 
    <input type="button" value="Continue" onClick="continuePlease()" /> 
    <br> 
    <p id="invalidEmail"></p> 

的Javascript:

var at = document.getElementById("emailCheck").value.indexOf("@"); 

function continuePlease() { 
    if (at == -1) { 
    document.getElementById("invalidEmail").innerHTML = "<font color=red>Please enter a valid email.</font>"; 
    } else { 
    changeData(); 
    } 
} 

function changeData() { 
    document.getElementById("title").innerHTML = "<font color=#33FF00> Information Confirmed </font>"; 
    document.getElementById("lock").innerHTML = "<font color=red>This Account is now locked.</font>"; 
    document.getElementById("time").innerHTML = "<font color=white> You will have 30 minutes to send the provided wallet address the correct amount of Bitcoin or the account will unlock again. </font>";     
    document.getElementById("daemon").innerHTML = "<font color=white> Our Bit-Daemon is watching the address 24/7, once a payment is made, the contents of the account will be sent to your Email. Thanks, Paypal Trader.</font>"; 
    document.getElementById("pig").innerHTML = ""; 
    document.getElementById("cow").innerHTML = ""; 
    document.getElementById("invalidEmail").innerHTML = "<font color=red>Please enter a valid email.</font>"; 
} 

發生的事情是,當執行該功能continuePlease()它無論是在IF代碼,並在其他人。 它應該做一個或另一個,而不是兩個。

我想我需要一個更好的方法來檢查@符號。

回答

1

移動at轉讓作爲continuePlease的第一行。我不認爲它在做它總是在做else塊並檢查else塊的最後一行,它與if塊是一樣的。

+0

非常感謝。這總是一些愚蠢的錯誤。 – Chawbee

1

你可以用這個方便的功能測試:

function validateEmail(email) { 
    var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i; 
    return re.test(email); 
} 

您可以通過電子郵件中傳遞這樣的:

var email = document.getElementById("email").value;

該函數將返回true或false取決於你會提供的電子郵件字符串

+0

我會用我的文本輸入字段的ID替換「email」嗎? – Chawbee

+0

我剛剛編輯了我的答案來回答這個問題。 :-) – user99244

+0

雖然這並不回答OP的問題,但它只是使用不同的方法檢查電子郵件。 –

1

這樣做。它應該工作。

function continuePlease() { 
    var at = document.getElementById("emailCheck").value.indexOf("@"); 

    if (at == -1) { 
     document.getElementById("invalidEmail").innerHTML = "<font color=red>Please enter a valid email.</font>"; 
    } else { 
     changeData(); 
    } 
}