2016-08-11 30 views
0

即使沒有用戶在電子郵件字段中輸入「@」,我的表單也會發送。如何驗證表單中的電子郵件

如果沒有「@」,我該如何離開紅色邊框?

<input type="text" name="Name" placeholder="Nome..." class="quote-form-element" /> 
<input type="text" name="City" placeholder="Cidade/UF..." class="quote-form-element quote-form-client-email last" /> 
<input type="text" name="phone" placeholder="Telefone..." class="quote-form-element" /> 
<input type="text" name="email" placeholder="E-mail..." class="quote-form-element quote-form-client-email last" /> 
+0

寫入代碼? 'if(無效字段){標記字段紅色; '',基本上。 –

+1

這已經在這裏回答http://stackoverflow.com/questions/19605773/html5-email-validation – gautsch

+2

可能重複的[在JavaScript驗證電子郵件地址?](http://stackoverflow.com/questions/46155/validate-電子郵件地址中的JavaScript) –

回答

0

將它變成函數並使用正則表達式來檢查電子郵件是否有效。

function emailVal(email){ 
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/; 

    if (reg.test(emailField.value) == false) 
    { 
     //invalid email 
     return false; 
    } 

    return true; 

} 

在文本框中使用:

<input type="email" onblur="emailVal(this)"> 

現在,如果emailVal()返回false,做一個紅色邊框圍繞文本框。