[英]How to check whether an email address contains an @ sign and a '.'. in html and javascript
[英]Check if a string contains an email address?
如何檢查以驗證給定的字符串是否包含電子郵件地址。
電子郵件地址也將包含在許多其他文本中。
此外,不必嚴格驗證電子郵件地址本身。 更a@b.xyz
只是想確保a@b.xyz
存在。
示例字符串:
Overall I liked the service, but had trouble using the widget generator.
Want more info? You can contact me at bob@example.org.
普通的 javascript 很好,但我碰巧正在使用 jQuery,所以如果有某種幫助函數可以使這更容易......去吧。
function checkIfEmailInString(text) {
var re = /(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))/;
return re.test(text);
}
你可以使用這個:
var StrObj = "whatever, this is my email dave@gmail.com other text";
var emailsArray = StrObj.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
if (emailsArray != null && emailsArray.length) {
//has email
}
如果需要,這還可以讓您從數組中獲取電子郵件地址。
嘗試
/\b[a-z0-9-_.]+@[a-z0-9-_.]+(\.[a-z0-9]+)+/i.test(text)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.