본문 바로가기
웹 개발 이야기/js, jquery

[JS] email 정규식

by Gommin 2023. 5. 31.

// HTML

<div class="col-sm-9">
    <input type="email" name="mt_id" id="mt_id" value="" class="form-control form-control-sm" oninput="validEmail(this.value)" />
    <small id="mt_id_help" class="form-text text-muted"></small>
</div>

 

// javascript

function validEmail(obj){
    if(validEmailCheck(obj)==false){
        $("#mt_id_help").html('* 이메일 형식으로 입력해주세요. ex) xxx@000.000');
        $("#mt_id_help").show();
    }else{
        $("#mt_id_help").hide();
    }

    if(!obj){
        $("#mt_id_help").hide();
    }
}

function validEmailCheck(obj) {
    const pattern = /^[0-9a-zA-Z]([-_.]?[0-9a-zA-Z])*@[0-9a-zA-Z]([-_.]?[0-9a-zA-Z])*.[a-zA-Z]{2,3}$/i;
    return (obj.match(pattern) != null);
}

댓글