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