[英]How to validate an email on input field with regex?
我正在創建一個帶有驗證的 email 表單。 所以我現在所做的可以支持包括@
和.
但是前綴中有一些條件和域名無效,這對我來說應該是有效的。
例如,如果我輸入myemail@gmail.com
, my_email@gmail.com
是有效的。 但是,如果我輸入my-email@gmail.com
, my.email@gmail.com
, myemail@custom.co.id
它是無效的。 什么是正確的正則表達式才能使 email 模式有效?
$("[name='email']").on("focusout", function(){ let regex = /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,5}$/ let getInputValue = $(this).val() if(getInputValue.match(regex)){ $(".error").html("valid email") }else{ $(".error").html("invalid email") } })
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="text" name="email"> <div class="error"></div>
嘗試這個
$("[name='email']").on("focusout", function(){ let regex = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/ let getInputValue = $(this).val() if(getInputValue.match(regex)){ $(".error").html("valid email") }else{ $(".error").html("invalid email") } })
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="text" name="email"> <div class="error"></div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.