使用外部 javascript 的 html 表单验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8650732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 04:06:35  来源:igfitidea点击:

html form validation using external javascript

javascripthtml

提问by niceApp

Friends,

朋友们,

I am trying to validate my html form using an external javascript but I am getting an error. Here are the code details:

我正在尝试使用外部 javascript 验证我的 html 表单,但出现错误。以下是代码详情:

<html>
    <head>
        <title>Contact US</title>
        <link href="StyleSheet.css" rel="Stylesheet" type="text/css" />
        <script type = "text/javascript" src = "JavaScript.js"> </script>
    </head>
    <body>
    <h1>Contact Us</h1>
    <form id = "contactUs" action="Index.htm"  onsubmit = "return validateFirstName()">
    <div class="row">
                <span class = "label" >First Name:</span>
                <span class = "formw"><input type="text" name = "fname"/> </span>
     <span class = "formw"><input type="submit"  name = "btnSubmit" value = "Submit" /> </span>
    </div>
</form>
</body>
</html>

My external javascript file "JavaScript.js" contains the following code:

我的外部 javascript 文件“JavaScript.js”包含以下代码:

  function validateFirstName() {
        var x = document.forms["contactUs"]["fname"].value;
        if (x == null || x == "") {
            alert("First name cannot be left blank.");
            return false;
        }
        else {
            return true;
        }

but when I clicked the Submit button then I get the following error:

但是当我单击“提交”按钮时,出现以下错误:

Line: 19 Error: 'validateFirstName' is undefined

第 19 行错误:'validateFirstName' 未定义

Thank You for your help in advance.

提前谢谢你的帮助。

回答by Will

You need a closing curly brace on your function.

你的函数需要一个右花括号。

function validateFirstName() {
    var x = document.forms["contactUs"]["fname"].value;
    if (x == null || x == "") {
        alert("First name cannot be left blank.");
        return false;
    }
    else {
        return true;
    }
} // <- here

回答by Eimantas Kasperiūnas

Heres my code I had wrote for checking if email inserted is valid. You can customize it to your needs, might be usefull :)

这是我编写的用于检查插入的电子邮件是否有效的代码。您可以根据自己的需要对其进行自定义,可能会很有用 :)

  function readdata()
  {
        a=document.getElementById('email')
        k=a.value
        //document.write(k)
alert(k);
        }

    function emailValidation(elem, helperMsg) {
            var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
            if(elem.value.match(emailExp)){
                            return true;
                            }
                            else{
                            alert(helperMsg);
                            elem.focus();
                            return false;
                            }
    }

Also looking at your code, you lack for curly brace at the end of your JavaScript

同时查看您的代码,您的 JavaScript 末尾缺少花括号