JS验证用户输入的电子邮箱和密码是否合法

  • 作者:村长
  • 发布时间:2020-05-08 13:37
  • 分类:JavaScript
  • 标签: 热门推荐

JS实现简单的检查邮件格式是否正确:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>检查邮箱密码</title>
</head>
<body>
    
    <form action="http://www.shuaiqi100.com" onsubmit="checkForm()">
        邮件:<input type="text" name="email" id="email"><br/>
        密码:<input type="password" name="Pass" id="pass"><br/>
        <input type="submit" value="密码">
    </form>

    <script>
        function checkForm()
        {
            var email = document.getElementById("email").value;
            var pass = document.getElementById("pass").value;

            //判断邮件格式是否找到@字符
            if(email.indexOf("@") == -1)
            {
                alert("请输入正确的邮箱格式!");
                event.returnValue = false;
                document.getElementById("email").focus();
                return;
            }
            if(email.indexOf(".") == -1)
            {
                alert("请输入正确的邮箱格式!");
                event.returnValue = false;
                return;
            }

            //检查密码是否符合条件
            if(pass.length < 6)
            {
                alert("密码长度不能小于6!");
                event.returnValue = false; 
                return;
            }
        }

    </script>

</body>
</html>


  • php程序员面试笔试宝典
琉忆编程库
加载中...
TOP