search

Home  >  Q&A  >  body text

javascript - 请问调用alert()方法为什么会出现提示两次的情况?

1.调用alert()方法出现两次相同的提示信息,如果有多个alert()调用,会在开头出现一次,结尾出现一次。
<!DOCTYPE html PUBLIC "-//W3C//DTD XH TML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml&...
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script language="javascript">
function RegEx()
{

1

2

3

4

5

6

<code>checkName();

checkAge();

checkId();

checkEmail();

if(checkName()&&checkAge()&&checkId()&&checkEmail())

  alert("恭喜您,输入全部正确");  </code>

}
function checkName()
{

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<code>var Name=document.getElementById("UserName").value;

var reg=/^[a-zA-Z][a-zA-Z0-9_]*$/;

if(Name!="")

{

    if(!reg.test(Name))

    {       

      alert("您输入的用户名不合法!请以字母开头"); 

      document.getElementById("UserName").focus();

      return false;

    }

}

else

{

    alert("请输入用户名");

    return false;

}

return true;</code>

}
function checkAge()
{

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<code>var Age=document.getElementById("UserAge").value;

if(Age!="")

{

    if(Age<=0||Age>100)

    {       

          alert("您输入的年龄不合法!请输入1-100"); 

          document.getElementById("UserAge").focus();

          return false;

    }   

}

else

{

    alert("请输入年龄");

    return false;

}

return true;</code>

}
function checkId()
{

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<code>var Id=document.getElementById("UserId").value;

var reg=/(^\d{16}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;

if(Id!="")

{

    if(!reg.test(Id))

    {

          alert("您输入的身份证不合法!请输入16位或18位身份证号"); 

          document.getElementById("UserId").focus();

          return false;

    }

}

else

{

    alert("请输入身份证号码");

    return false;

}

return true;</code>

}
function checkEmail()
{

1

2

3

4

5

6

7

8

9

<code>var email = document.getElementById("UserEmail").value;   

var reg =  /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/ ;   

if (email != "") {

    if(!reg.test(email))   

     {       

      alert("您输入的电子邮件地址不合法!"); 

      document.getElementById("UserEmail").focus();

      return false;

     }    </code>

}
else

1

2

3

4

<code>{

    alert("请输入电子邮件");

    return false;

}</code>

return true;
}
</script>
</head>

<body>

昵称: <label for="name"></label> <input type="text" name="name" id="UserName" />
年龄: <label for="age"></label> <input type="text" name="age" id="UserAge" />
身份证: <label for="userId"></label> <input type="text" name="userId" id="UserId" />
邮箱: <label for="Email"></label> <input type="text" name="Email" id="UserEmail" />
<p align="center"> <input type="submit" name="confirm" id="confirm" value="提交" onclick="RegEx()" /> </p>

</body>
</html>

PHPzPHPz2879 days ago830

reply all(2)I'll reply

  • PHPz

    PHPz2017-04-11 13:34:13

    在RegEx()函数里,前四行已经调用了检测函数,if中又再次调用,去掉前四行就行。

    reply
    0
  • PHPz

    PHPz2017-04-11 13:34:13

    前面已经执行一次,if中又执行了一次。

    1

    2

    3

    4

    5

    6

    7

    8

    9

    <code>function RegEx()

    {

        var a=checkName();

        var b=checkAge();

        var c=checkId();

        var d=checkEmail();

        if(a && b && c && d)

        alert("恭喜您,输入全部正确");

    }</code>

    reply
    0
  • Cancelreply