You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

HTML表单OnSubmit报错ReferenceError: sendMsg未定义,求助解决

解决"ReferenceError: sendMsg is not defined"及代码报错问题

你的代码存在多个语法和概念错误,直接导致函数无法被识别,以下是逐个修复点:

1. 脚本标签类型错误

你写的<script type="javascript/text">是错误格式,正确的MIME类型为text/javascript,现代浏览器可直接省略type属性:

<script>
// 函数代码
</script>

2. JavaScript无public关键字

JS声明函数不需要public修饰符,直接删除即可:

function sendMsg() {
  // 函数逻辑
}

3. 语法错误修复

  • JS中不等于运算符是!==,而非<>,将telNo <> ""改为telNo !== ""
  • 第三个if语句缺少闭合括号,且逻辑判断错误:原本的正则匹配逻辑写反了,应该判断邮箱不匹配正则时触发错误,修正为:
    if (email === "" || !regEmail.test(email))
    

4. const变量无法重新赋值

你用const send = true;声明变量后又尝试send = false;,这会触发赋值错误,将const改为let:

let send = true;

5. 正则表达式全局标志问题

全局匹配的正则(带g标志)使用test方法会有状态残留,导致多次验证结果异常,去掉所有正则的g标志:

var regEmail=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/; 
var regPhone=/^\d{10}$/;                       
var regName = /\d+$/; 

6. 表单提交返回值

函数末尾需要添加return send;,这样验证不通过时会阻止表单默认提交行为,确保验证逻辑生效。


修复后的完整代码

<!DOCTYPE html >
<html lang="en">
<head>
    <!-- email from javascript -->
    <script>
        // import {GenericEmail} from '../../js/Email/ElasticEmail.js';
        
        function sendMsg() 
        {
            var regEmail=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/; 
            var regPhone=/^\d{10}$/;                       
            var regName = /\d+$/; 
            
            let send = true;
            
            const name = document.forms.ContactForm.ContactName.value;
            const telNo = document.forms.ContactForm.ContactTel.value;
            const email = document.forms.ContactForm.ContactEmail.value;
            const msg = document.forms.ContactForm.ContactMsg.value;
            
            if (name === "" || regName.test(name))
            {
                send = false;
                document.forms.ContactForm.ContactName.placeholder = "Name is required";
            }
            
            if (telNo !== "" && !regPhone.test(telNo))
            {
                send = false;
                document.forms.ContactForm.ContactTel.placeholder = "Valid phone number required";
            }
            
            if (email === "" || !regEmail.test(email))
            {
                send = false;
                document.forms.ContactForm.ContactEmail.placeholder = "Email address is required";
            }
            
            if (msg.trim().length === 0)
            {
                send = false;
                document.forms.ContactForm.ContactMsg.placeholder = "Say something...";
            }
            
            if (send)
            {
                const sender = new GenericEmail();
                const result = sender.SendMail("YourNuttyLady", name, telNo, email, msg);
                if (result === "ok")
                {
                    alert("Message was sent !");
                }
            }
            return send;
        }
    </script>
</head>
<body>
    <div class="page-wrapper">
        <!-- Contact Us -->
        <form class="submission-form" name="ContactForm" onsubmit="return sendMsg()" method="POST">
            <input type="text" id="first-name" name="ContactName" placeholder="Enter your name"> </br>
            <input type="tel" id="phone" name="ContactTel" placeholder="Enter your phone number">   </br>
            <input type="email" id ="email" name="ContactEmail" placeholder="Enter your email"></br>
            <textarea id="message" name="ContactMsg" placeholder="Enter your message"></textarea>
            <input type="submit" id="sendBtn" value="send" name="ContactSend">
        </form>
    </div>
</body>
</html>

内容的提问来源于stack exchange,提问作者AlexC

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 03:25:07