Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.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_Validation - Fatal编程技术网

Javascript 停止数值&允许数值

Javascript 停止数值&允许数值,javascript,validation,Javascript,Validation,我在我的页面中添加了一些JavaScript验证,发现我找不到任何有用的来源来告诉我如何停止数值并允许它们出现在不同的输入框中。我对JavaScript非常陌生,还没有完全掌握它。我知道VB有一个类似于我要求的命令: 下面是我想要停止数值的代码: if (document.ExamEntry.name.value=="") { alert("You must enter your name \n"); document.ExamEntry.name.focus(); document.getEl

我在我的页面中添加了一些JavaScript验证,发现我找不到任何有用的来源来告诉我如何停止数值并允许它们出现在不同的输入框中。我对JavaScript非常陌生,还没有完全掌握它。我知道VB有一个类似于我要求的命令:

下面是我想要停止数值的代码:

if (document.ExamEntry.name.value=="") {
alert("You must enter your name \n");
document.ExamEntry.name.focus();
document.getElementById('name').style.color="red";
result = false;
}

if (document.ExamEntry.subject.value=="") {
alert("You must enter the subject \n");
document.ExamEntry.subject.focus();
document.getElementById('subject').style.color="red";
result = false;
}
if (document.ExamEntry.CadNumber.value.length!== 4) {
alert("Make sure you only have 4 numbers! \n");
document.ExamEntry.CadNumber.focus();
document.getElementById('CadNumber').style.color="red";
result = false;
}
下面是我只想在中允许数值的代码:

if (document.ExamEntry.name.value=="") {
alert("You must enter your name \n");
document.ExamEntry.name.focus();
document.getElementById('name').style.color="red";
result = false;
}

if (document.ExamEntry.subject.value=="") {
alert("You must enter the subject \n");
document.ExamEntry.subject.focus();
document.getElementById('subject').style.color="red";
result = false;
}
if (document.ExamEntry.CadNumber.value.length!== 4) {
alert("Make sure you only have 4 numbers! \n");
document.ExamEntry.CadNumber.focus();
document.getElementById('CadNumber').style.color="red";
result = false;
}
--编辑--

这是我到目前为止所得到的,它的工作方式有点像现在不断出现的。。。我想知道你是否还知道

停止数值:

if (document.ExamEntry.subject.value) {
isNaN(parseInt(1));
alert("Please make sure you only have letters! \n");
document.ExamEntry.subject.focus();
document.getElementById('subject').style.color="red";
result = false;
}
if (document.ExamEntry.CadNumber.value) {
isNaN(parseInt(""));
alert("Please make sure you only have numbers! \n");
document.ExamEntry.CadNumber.focus();
document.getElementById('CadNumber').style.color="red";
result = false;
}
仅允许数值:

if (document.ExamEntry.subject.value) {
isNaN(parseInt(1));
alert("Please make sure you only have letters! \n");
document.ExamEntry.subject.focus();
document.getElementById('subject').style.color="red";
result = false;
}
if (document.ExamEntry.CadNumber.value) {
isNaN(parseInt(""));
alert("Please make sure you only have numbers! \n");
document.ExamEntry.CadNumber.focus();
document.getElementById('CadNumber').style.color="red";
result = false;
}
查找isNaN和parseInt-它们应该可以帮助您开始。从JS控制台

isNaN(parseInt("foo"))
true
isNaN(parseInt(12))
false
isNaN与VBA isNumeric相反,因此需要在document.ExamEntry.CadNumber.value上与parseInt一起使用

像这样使用它:

if (isNaN(parseInt(document.ExamEntry.CadNumber.value))) {
    alert("Please make sure you only have numbers! \n");
    document.ExamEntry.CadNumber.focus();
    document.getElementById('CadNumber').style.color="red";
    result = false;
}

要给出一个小例子说明它是如何工作的,您可以查看这个小片段

从某种意义上说,在您提交表单的那一刻,它将转到validate函数,然后该函数将检查表单的需求

“仅数字/仅文本”由类型暗示,您的浏览器也可以提供帮助,并且错误消息在标题中提供

当一个字段失败时,验证停止并抛出错误

注意,如果您有任何其他元素需要检查,比如选择,您仍然需要做一些额外的工作

如果您想了解有关可以设置的元素类型的更多信息,也可以检查它

函数validateform{ var=true, i、 len、item、itemArray、firstErrorField; 如果!表格{ 成功=错误; }否则{ itemArray=form.getElementsByTagName'input'; 对于i=0,len=itemArray.length;i这可能会有所帮助:但唯一的一件事是,我的整个项目必须使用JavaScript。学习多种语言会很好,但不是为了这个..你的if现在的结构是,如果它有一个值做你的解析,抛出一个警报。。。因此,如果不希望在添加值时经常运行,那么应该在if语句中包含parseInt/isNan检查。顺便说一句,不要用现在的方式获取元素,充其量只能在internet explorer中工作,而要使用document.getElementById。。。只要小心编辑这里,不要把它变成变色龙问题嗨,那里,谢谢你的输入!所以我会把一个If语句放在一个If语句中?我不太清楚你在说什么。那么在我提供的代码中它是如何构造的呢?我把这当作一个挑战留给你自己,来帮助你学习JS:-你需要的几乎所有东西都在我的答案中。试一试,如果你的代码不正确,请发回你的代码,然后我们可以帮助你调整它。你好,谢谢你的输入!我添加了一个更新来解释现在发生的事情:嗯,不完全是!但是很好的尝试。我已经添加了一个示例来演示如何使用它。太好了!如果它对你有效,你应该接受这个答案,这有两个方面的帮助:1。其他人知道不要来添加更多的答案,和2。其他有类似问题的人将能够使用您接受的答案来帮助自己。