Validate email address textbox using JavaScript

前端 未结 10 865
一个人的身影
一个人的身影 2020-12-02 17:03

I have a requirement to validate an email address entered when a user comes out from the textbox.
I have googled for this but I got form validation JScript; I don\'t wa

10条回答
  •  星月不相逢
    2020-12-02 17:44

    Validating email is a very important point while validating an HTML form. In this page we have discussed how to validate an email using JavaScript :

    An email is a string (a subset of ASCII characters) separated into two parts by @ symbol. a "personal_info" and a domain, that is personal_info@domain. The length of the personal_info part may be up to 64 characters long and domain name may be up to 253 characters. The personal_info part contains the following ASCII characters.

    1. Uppercase (A-Z) and lowercase (a-z) English letters.
    2. Digits (0-9).
    3. Characters ! # $ % & ' * + - / = ? ^ _ ` { | } ~
    4. Character . ( period, dot or fullstop) provided that it is not the first or last character and it will not come one after the other.

    The domain name [for example com, org, net, in, us, info] part contains letters, digits, hyphens, and dots.

    Example of valid email id

    mysite@ourearth.com

    my.ownsite@ourearth.org

    mysite@you.me.net

    Example of invalid email id

    mysite.ourearth.com [@ is not present]

    mysite@.com.my [ tld (Top Level domain) can not start with dot "." ]

    @you.me.net [ No character before @ ]

    mysite123@gmail.b [ ".b" is not a valid tld ]

    mysite@.org.org [ tld can not start with dot "." ]

    .mysite@mysite.org [ an email should not be start with "." ]

    mysite()*@gmail.com [ here the regular expression only allows character, digit, underscore, and dash ]

    mysite..1234@yahoo.com [double dots are not allowed]

    JavaScript code to validate an email id

    function ValidateEmail(mail) {
            if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w {2, 3})+$/.test(myForm.emailAddr.value)) {
                return (true)
            }
            alert("You have entered an invalid email address!")
            return (false)
        }
    

提交回复
热议问题