如何使用运行验证简单地发送消息HTML

时间:2015-10-11 23:49:41

标签: javascript html

我认为我的网页已经完成了很多逻辑,我现在唯一的问题是“发送”按钮不会执行javascript“alert()”功能。

这是我的HTML代码:

<body>
<h1>Send us a message</h1>
<form name = "info">
Name: <input type = "text" id = "inf" name = "name" placeholder="Enter a valid name" >*</br></br>
Email Address: <input type="email" id = "inf" name="email" placeholder="Enter a valid email address" >*</br></br>
Phone Number: <input type = "tel" id = "inf" name ="PN"  placeholder="Enter a valid phone number" >*</br></br>
<textarea name="Text1" cols="40" rows="5" placeholder="Write Your Message Here..."></textarea><br><br>
<input type ="button" value = "Send" onclick="alert()">
</form>
</body>

这是我的Javascript代码:

<script type = "text/javascript">
function ValEntry(){
    var valid = true;

    if ( document.info.name.value== ""||document.info.email.value== ""||document.info.PN.value == "" )
    {
        alert ( "Please fill up the remaining text boxes" );
        valid = false;
    }
    return valid;
}

function checkemail(){
    var testresults1
    var str=document.info.email.value
    var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i

    if (filter.test(str))
    testresults1=true
    else{
    alert("Please input a valid email address!")
    testresults1=false
    }
return (testresults1)
}

function checkphone(){
    var testresults2
    var int = document.info.PN.value
    var filter2= /[1-9]/g;

    if (filter2.test(int))
    testresults2=true
    else{
    alert("Please input a valid phone number!")
    testresults2=false
}
return (testresults2)
}

function validate(){
    if (document.info.email.value)
    return checkemail()
    else{
    return true
    }       
}

function validate2(){
    if (document.info.PN.value)
    return checkphone()
    else{
    return true
    }       
}

function FinalVal(){
    return ValEntry() && validate() && validate2();

}

function alert(){
    FinalVal();
    {
       var fields=["name"];
       var msg = "";
       msg += "The message has been sent " + document.info[ fields ].value + "!";
       alert("msg");
    }
    return true;
}


</script>

1 个答案:

答案 0 :(得分:0)

问题在于你对alert()的调用。我敢打赌,当你点击按钮时,你只是得到一个空警报框...

<input type ="button" value = "Send" onclick="alert()">

这是一个内置的javascript函数,您尝试使用相同的名称定义自己的函数。将该功能重命名为几乎任何其他功能,它将对您有用。

<input type="button" value="Send" onclick="testSubmit()">

function testSubmit(){
    FinalVal();

    var fields=["name"];
    var msg = "";
    msg += "The message has been sent " + document.info[ fields ].value + "!";
    alert("msg");

    return true;
}