简体   繁体   中英

How to check if input looks like an email address and then activate submit button?

I use the following code to check if all form fields are not empty before the submit button is activated.

<script type="text/javascript">
            function checkForm() {
                var cansubmit = false;
                $('form input[type="text"]').each(function (index, element) {
                    if (element.value == "") {
                        cansubmit = true;
                    }
                });
                document.getElementById("uploadButton").disabled = cansubmit;
            }
        </script>

How can I edit this code so it checks if the email field contains an email address before the submit button is activated?

UPDATE

I changed the code to this and it seems to work. It checks if it contains @ (and not as the first symbol) and . (and not as the third symbol).

Is this a logical code or should it be easier?

<script type="text/javascript">
            function checkForm() {
                var cansubmit = false;
                $('form input[type="text"]').each(function (index, element) {
                    if (element.value == "") {
                        cansubmit = true;
                    }
                });
                $('form input[type="email"]').each(function (index, element) {
                    if (element.value.indexOf("@") <= 0) {
                        cansubmit = true;
                    }
                    else if (element.value.indexOf(".") <= 2) {
                        cansubmit = true;
                    }
                });
                document.getElementById("uploadButton").disabled = cansubmit;
            }
        </script>

您可以使用答案中的正则表达式来验证电子邮件地址:

if ((element.value == "" && element.type != "email") || (element.type == "email" && !/([!#-'*+/-9=?A-Z^-~-]+(\.[!#-'*+/-9=?A-Z^-~-]+)*|"([]!#-[^-~ \t]|(\\[\t -~]))+")@([0-9A-Za-z]([0-9A-Za-z-]{0,61}[0-9A-Za-z])?(\.[0-9A-Za-z]([0-9A-Za-z-]{0,61}[0-9A-Za-z])?)*|\[((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|IPv6:((((0|[1-9A-Fa-f][0-9A-Fa-f]{0,3}):){6}|::((0|[1-9A-Fa-f][0-9A-Fa-f]{0,3}):){5}|[0-9A-Fa-f]{0,4}::((0|[1-9A-Fa-f][0-9A-Fa-f]{0,3}):){4}|(((0|[1-9A-Fa-f][0-9A-Fa-f]{0,3}):)?(0|[1-9A-Fa-f][0-9A-Fa-f]{0,3}))?::((0|[1-9A-Fa-f][0-9A-Fa-f]{0,3}):){3}|(((0|[1-9A-Fa-f][0-9A-Fa-f]{0,3}):){0,2}(0|[1-9A-Fa-f][0-9A-Fa-f]{0,3}))?::((0|[1-9A-Fa-f][0-9A-Fa-f]{0,3}):){2}|(((0|[1-9A-Fa-f][0-9A-Fa-f]{0,3}):){0,3}(0|[1-9A-Fa-f][0-9A-Fa-f]{0,3}))?::(0|[1-9A-Fa-f][0-9A-Fa-f]{0,3}):|(((0|[1-9A-Fa-f][0-9A-Fa-f]{0,3}):){0,4}(0|[1-9A-Fa-f][0-9A-Fa-f]{0,3}))?::)((0|[1-9A-Fa-f][0-9A-Fa-f]{0,3}):(0|[1-9A-Fa-f][0-9A-Fa-f]{0,3})|(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3})|(((0|[1-9A-Fa-f][0-9A-Fa-f]{0,3}):){0,5}(0|[1-9A-Fa-f][0-9A-Fa-f]{0,3}))?::(0|[1-9A-Fa-f][0-9A-Fa-f]{0,3})|(((0|[1-9A-Fa-f][0-9A-Fa-f]{0,3}):){0,6}(0|[1-9A-Fa-f][0-9A-Fa-f]{0,3}))?::)|(?!IPv6:)[0-9A-Za-z-]*[0-9A-Za-z]:[!-Z^-~]+)])/.test(element.value))) {

Use a combination of <input type="email"> and inputElement.validity to check if the input is valid. Docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM