Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用外部javascript进行html表单验证_Javascript_Html - Fatal编程技术网

使用外部javascript进行html表单验证

使用外部javascript进行html表单验证,javascript,html,Javascript,Html,朋友们 我试图使用外部javascript验证我的html表单,但我遇到了一个错误。以下是代码详细信息: <html> <head> <title>Contact US</title> <link href="StyleSheet.css" rel="Stylesheet" type="text/css" /> <script type = "text/javascript"

朋友们

我试图使用外部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>
但当我单击“提交”按钮时,出现以下错误:

第19行 错误:“validateFirstName”未定义


提前感谢您的帮助。

您的函数需要一个右大括号

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
函数validateFirstName(){
var x=document.forms[“contactUs”][“fname”]值;
如果(x==null | | x==“”){
警报(“名字不能为空。”);
返回false;
}
否则{
返回true;
}

}//在函数上需要一个右大括号

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
函数validateFirstName(){
var x=document.forms[“contactUs”][“fname”]值;
如果(x==null | | x==“”){
警报(“名字不能为空。”);
返回false;
}
否则{
返回true;
}

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


再看看您的代码,您的JavaScript末尾缺少大括号

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


再看看您的代码,您的JavaScript末尾缺少大括号

谢谢。我花了好几个小时想弄清楚,但是弄不清楚。你几分钟就发现了。为您脱帽致敬。学习联系方式:谢谢。我花了好几个小时想弄清楚,但是弄不清楚。你几分钟就发现了。为您脱帽致敬。学习联系方式:谢谢,我也将使用电子邮件验证。谢谢,我也将使用电子邮件验证。
  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;
                            }
    }