2015-09-28 108 views
1

我想檢查我的字段是否爲零,然後返回true否則檢查電子郵件驗證。我的字段名稱是app_email。如何使用JavaScript檢查電子郵件驗證?

$.validator.addMethod("checkEmail", function(app_email, element) { 
     var result; 
     if (condition) { 
      result = (app_email === "NIL") ? true : false; 
     } else { 
      preg_match(/^([\w-\.][email protected]([\w-]+\.)+[\w-]{2,4})?$/,app_email); 
      return true; 
     } 
    }, 
    "Email id is not valid." 
); 

HTML

     <div class="control-group"> 
         <label class="control-label">E-mail ID<span class="required">*</span></label> 
          <div class="controls"> 
            <input type="text" class="span6 m-wrap" name="app_email" title="Enter Email Id"> 
          </div> 
         </div> 
+0

你想讓你的領域允許用戶,如果他沒有輸入任何東西..如果他進入shuld檢查驗證權嗎? – Webruster

+0

您可以使用單選按鈕檢查自定義驗證功能。看到我的答案[這裏](http://stackoverflow.com/questions/32816445/how-to-check-email-validation-using-js/32816896#32816896)與工作演示 –

+0

不能你只使用輸入類型的電子郵件,它會照顧休息 – Rex

回答

2

如果您正在使用jQuery驗證,你可以使用這樣的默認功能。您可以參考link

$("#myform").validate({ 
    rules: { 
    fieldNameHere: { 
     required: true, 
     email: true 
    } 
    } 
}); 

檢查值是否爲'NIL'返回true,否則驗證電子郵件。

$.validator.addMethod("checkEmail", 
    function(value, element) { 
     return value === "NIL" ? true : /^([\w-\.][email protected]([\w-]+\.)+[\w-]{2,4})?$/.test(value); 
    }, 
    "Email id is not valid." 
    ); 
+0

我想檢查我的領域是無 – robins

+0

你想檢查什麼,字段值或字段名稱? – janmvtrinidad

+0

我想檢查我的字段值是否爲零,這是允許的 – robins

2

Demo

您可以使用單選按鈕,檢查自定義的驗證裏面像下面。

JS:

jQuery.validator.addMethod("checkEmail", function (value, element) { 
     if ($('input:radio').is(':checked')) { 
      if (/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/.test(value)) { 
       return true; 
      } else {  
       return false; 
      } 
     } else { 
      return true; 
     } 

    }, "Email id is not valid."); 

    $('#myform').validate({ 
     rules: { 
      email: { 
       checkEmail: true 
      } 
     } 
    }); 

HTML:

<form id="myform"> 
    <input type="radio" value=""> 
    <input name="email" id="email" type="text"/> 
    <input type="submit" /> 
</form> 

在這個例子中,如果選擇了單選按鈕電子郵件驗證將被執行。

+0

類型不是收音機 – robins

+0

然後是什麼類型?我使用了一個單選按鈕和一個電子郵件文本輸入。如果您提供字段類型,我可以更新演示和回答 –

+0

輸入類型=文本 – robins