Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/442.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表单验证是';不工作,不知道为什么?_Javascript_Html_Regex_Function_Validation - Fatal编程技术网

外部Javascript表单验证是';不工作,不知道为什么?

外部Javascript表单验证是';不工作,不知道为什么?,javascript,html,regex,function,validation,Javascript,Html,Regex,Function,Validation,因此,我编写了一个Javascript外部文件,我将把它放在下面。它用于验证HTML站点/文件中表单中的输入。我还有更多的函数要添加,但在它正常工作之前,我不会担心它们。Javascript代码是: // JavaScript source code function fourDigit(ID) { "use strict"; var pattern = /^[1-9]\d{0,3}$/g; var result = pattern.test(document.getE

因此,我编写了一个Javascript外部文件,我将把它放在下面。它用于验证HTML站点/文件中表单中的输入。我还有更多的函数要添加,但在它正常工作之前,我不会担心它们。Javascript代码是:

// JavaScript source code

function fourDigit(ID)
{
    "use strict";
    var pattern = /^[1-9]\d{0,3}$/g;
    var result = pattern.test(document.getElementById(ID).value);

    if (result == false)
    {
        alert("Please insert a valid numeric value between 1-9999");
        return false;
    }
    else
    {
        return true;
    }
}

function runner()
{
    "use strict";
    fourDigit('RunnerID');
}

function event()
{
    "use strict";
    fourDigit('EventID');
}

function validate()
{
    "use strict";
    var result = runner() && event();

    if (result == true)
    {
        alert("all good");
        return true;
    }
    else
    {
        alert("doesn't work");
        return false;
    }
}
当我在HTML文件上测试时(我调用表单提交按钮上的
validate()
函数),它总是向我发出
警报(“请在1-9999之间插入一个有效的数值”)
警报(“不工作”)即使我在RunnerID和EventID表单字段中插入了有效数字。我不明白为什么会有这个问题。可能很简单,但最近才开始学习Javascript

我调用的HTML文件是:

<!DOCTYPE html PUBLIC
  "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-type" content="text/html; charset=iso-8859-1"/>
<title>Submit a runner time</title>
<script language="javascript" src="validate.js"></script>
</head>
<body>
<hr/>
<h1>Submit a runner time</h1>
<hr/>
Note: all fields marked '*' are mandatory.
<p/>
<form onSubmit="return validate()"> 
<table>
<tr><td>Runner ID*</td>
<td><input type="text" id="RunnerID" name="RunnerID" size="4" maxlength="4"/></td>
</tr>
<tr><td>Event ID*</td>
<td><input type="text" id="EventID" name="EventID" size="4" maxlength="4"/></td>
</tr>
<tr><td>Date (YYYY-MM-DD)*</td>
<td><input type="text" name="Date" size="10" maxlength="10"/></td>
</tr>
<tr><td>Finish time (HH:MM:SS)*</td>
<td><input type="text" name="FinishTime" size="8" maxlength="8"/></td>
</tr>
<tr><td>Position</td>
<td><input type="text" name="Position" size="5" maxlength="5"/></td>
</tr>
<tr><td>Category ID</td>
<td><input type="text" name="CategoryID" size="3" maxlength="2"/></td>
</tr>
<tr><td>Age grade</td>
<td><input type="text" name="AgeGrade" size="5" maxlength="5"/></td>
</tr>
<tr><td>Personal best</td>
<td><input type="text" name="PB" size="1" maxlength="2"/></td>
</tr>
</table>
<input type="submit" name="submitrunnertime" value="submit"/>
<hr/>
</form>
</body>
</html>

提交跑步者时间

提交跑步者时间
注:所有标有“*”的字段都是必填字段。

跑步者ID* 事件ID* 日期(YYYY-MM-DD)* 完成时间(小时:毫米:秒)* 位置 类别ID 年龄等级 个人最佳状态


谢谢你的正则表达式错了

[1-9999]
表示1到9之间的1位数字

您需要
^[1-9]\d{0,3}$

意思是:

^
:字符串的开头

[1-9]
:1-9中的1位数字

\d
:一个数字

{0,3}
:0到3位数长

$
:字符串结尾

这将不允许使用纯JS

// JavaScript source code

function fourDigit(ID)
{
    var pattern = /^([1-9][0-9]{0,3})$/;;
    var result = pattern.test(document.getElementsByName(ID)[0].value);

    if (result == false)
    {
        alert("Please insert a valid numeric value between 1-9999");
        return false;
    }
    else
    {
        return true;
    }
}

function runner()
{
    return fourDigit('RunnerID');
}

function event()
{
    return fourDigit('EventID');
}

function validate()
{
    var result = runner() && event();

    if (result == true)
    {
        alert("all good");
        return true;
    }
    else
    {
        alert("doesn't work");
        return false;
    }
}

你在哪里调用你的脚本?您可以共享它吗?是的,您的文本字段需要
id=
您只声明
name=
我无法获取的内容您从document.GetElementById()中获取值,并在文本字段中使用name。我是瞎了还是javascript引入了新的东西?请不要破坏你的帖子。说真的,不要破坏帖子。好的,太好了,我会把它分类的。问题是,由于某种原因,故障仍然存在。感谢[1-9999]表示1到9之间的1位数字,或字符9,或字符9或字符9。问题是,我想我需要使用正则表达式,因为这是我一直在学习的内容。不过,我很欣赏这个替代解决方案,谢谢。我认为他没有使用jquery,他需要一个javascript解决方案,而你需要jquery。也许它也会有效:)我用纯JS更新我的代码。和使用正则表达式。试试看。当我使用它时,我甚至没有收到警报,尽管我不确定为什么,因为您的输入使用的是
name=
作为ID,而不是
ID=
。所以这个
getElementById
是错误的。