2015-08-21 95 views
1

我是新來的歐芹。香菜電子郵件或電話號碼

我想驗證一個區域或者是電子郵件地址或9 didgit數

HTML

<form id="registerform"> 
 
    <h1> Register form </h1> 
 
    <input type="text" id="memberdata" required> 
 
    <input type="submit"> 
 
    </form>

$(document).ready(function() { 
 
    
 
    
 
    
 
    $("#registerform").submit(function(event) { 
 
\t \t 
 
\t $('#registerform').parsley(); \t 
 
     var data = $("#memberInfo", this).val(); 
 
    
 
     callRegisterAPI(data, type); 
 
    }); 
 

 

 

 
});

如何是驗證這一點以及調用callRegisterAPI成功的最佳方式。

謝謝

回答

0

使用正則表達式。

9位數爲/^\d{9}$/

電子郵件看似簡單,但實際上是相當複雜的,特別是考慮到IDN字符

例如,почта@макс.укр是有效和工作電子郵件地址。實際上,你可以發郵件給我:-)使用它不幸的是很多網站不驗證它作爲有效的電子郵件地址

http://ex-parrot.com/~pdw/Mail-RFC822-Address.html

(?:(?:\r\n)?[ \t])*(?:(?:(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t] 
)+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?: 
\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(
?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ 
\t]))*"(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\0 
31]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\ 
](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+ 
(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\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:\r\n)?[ \t]) 
+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\ 
.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z 
|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*\>(?:(
?:\r\n)?[ \t])*))*)?;\s*) 
0

有沒有內置的驗證器來做到這一點,但有一點JavaScript你可以輕鬆地編寫您自己的自定義驗證器。

例如,使用V2.2的全新API,這裏的依賴香菜一個可愛的方式來驗證兩個「虛擬」的投入(未經測試):

var dummyEmail = $('<input data-parsley-type="email">').parsley(); 
var dummyNineDigits = $('<input data-parsley-pattern="\d{9}">').parsley(); 

Parsley.addValidator('emailorid', { 
    validateString: function(data) { 
    return dummyEmail.isValid(true, data) || dummyNineDigits.isValid(true, data); 
    }, 
    messages: { 
    en: "Is neither a nine digit long number nor a valid email address" 
    } 
}; 

然後,只需在任何領域使用data-parsley-emailorid你想驗證這種方式。

相關問題