未显示表单验证Javascript的错误消息

未显示表单验证Javascript的错误消息,javascript,validation,Javascript,Validation,我不确定我做错了什么。但是我的两个错误没有显示,nPrice和nAmount。但是,我的名字和nSmokes会显示出来。我想这和我使用的if-else的多重条件有关 我真的很想知道你的想法 提前谢谢 function doValidate() { //alert("Working"); document.myForm.nPrice.value=parseFloat(document.myForm.nPrice.value); document.myForm.nAmount.value=par

我不确定我做错了什么。但是我的两个错误没有显示,nPrice和nAmount。但是,我的名字和nSmokes会显示出来。我想这和我使用的if-else的多重条件有关

我真的很想知道你的想法

提前谢谢

function doValidate()
{

//alert("Working");

document.myForm.nPrice.value=parseFloat(document.myForm.nPrice.value);
document.myForm.nAmount.value=parseInt(document.myForm.nAmount.value);
document.myForm.nSmokes.value=parseInt(document.myForm.nSmokes.value);

//alert(document.myForm.nPrice.value);

//alert(document.myForm.nAmount.value);

//alert(document.myForm.nSmokes.value);



errorCount = 0;
errorMsg = ""

if( document.myForm.name.value == "") {

        errorMsg = errorMsg + "You need to enter a value!";
        document.getElementById('errors').innerHTML= errorMsg;
        errorCount++; 

}

if (document.myForm.nPrice.value < 0 && document.myForm.nPrice.value > 20.00) {

        errorMsg = errorMsg + "You need to enter a value between 0 and 20!";
        document.getElementById('errors').innerHTML= errorMsg;
        errorCount++; 
}

if (document.myForm.nAmount.value < 0 && document.myForm.nAmount.value > 40) {

        errorMsg = errorMsg + "You need to enter a value between 0 and 40!";
        document.getElementById('errors').innerHTML= errorMsg;
        errorCount++; 
}
if (document.myForm.nSmokes.value < 0) {

        errorMsg = errorMsg + "<br/>Either your doing a great job or you put in that you     smoked 0 today!";
        document.getElementById('errors').innerHTML= errorMsg;
        errorCount++; 
}

if (errorCount > 0) {
        return false;
} else {
        return true;
}
}
函数doValidate()
{
//警惕(“工作”);
document.myForm.nPrice.value=parseFloat(document.myForm.nPrice.value);
document.myForm.nAmount.value=parseInt(document.myForm.nAmount.value);
document.myForm.nSmokes.value=parseInt(document.myForm.nSmokes.value);
//警报(document.myForm.nPrice.value);
//警报(document.myForm.nAmount.value);
//警报(document.myForm.nSmokes.value);
errorCount=0;
errorMsg=“”
如果(document.myForm.name.value==“”){
errorMsg=errorMsg+“您需要输入一个值!”;
document.getElementById('errors')。innerHTML=errorMsg;
errorCount++;
}
如果(document.myForm.nPrice.value<0&&document.myForm.nPrice.value>20.00){
errorMsg=errorMsg+“您需要输入一个介于0和20之间的值!”;
document.getElementById('errors')。innerHTML=errorMsg;
errorCount++;
}
if(document.myForm.nAmount.value<0&&document.myForm.nAmount.value>40){
errorMsg=errorMsg+“您需要输入一个介于0和40之间的值!”;
document.getElementById('errors')。innerHTML=errorMsg;
errorCount++;
}
if(document.myForm.nSmokes.value<0){
errorMsg=errorMsg+“
要么你做得很好,要么你今天抽了0支烟!”; document.getElementById('errors')。innerHTML=errorMsg; errorCount++; } 如果(错误计数>0){ 返回false; }否则{ 返回true; } }
HTML


按你的名字戒烟
你的名字
你辞职的日期
一包的价格
一包多少
你每天吸烟的数量

document.myForm.nPrice.value<0&&document.myForm.nPrice.value>20.00
一个值怎么可能小于0并且大于20? 你正在那里找一个手术室

document.myForm.nPrice.value < 0 || document.myForm.nPrice.value > 20.00

document.myForm.nPrice.value<0 | | document.myForm.nPrice.value>20.00
介于0到20之间

document.myForm.nPrice.value > 0 && document.myForm.nPrice.value < 20.00

document.myForm.nPrice.value>0&&document.myForm.nPrice.value<20.00

第二个if表达式也犯了类似的错误

document.myForm.nPrice.value<0&&document.myForm.nPrice.value>20.00
一个值怎么可能小于0并且大于20? 你正在那里找一个手术室

document.myForm.nPrice.value < 0 || document.myForm.nPrice.value > 20.00

document.myForm.nPrice.value<0 | | document.myForm.nPrice.value>20.00
介于0到20之间

document.myForm.nPrice.value > 0 && document.myForm.nPrice.value < 20.00

document.myForm.nPrice.value>0&&document.myForm.nPrice.value<20.00

第二个if表达式也犯了类似的错误

这一行不会像你想的那样起作用:

document.myForm.nSmokes.value = parseInt(document.myForm.nSmokes.value);
一旦您再次将值放回表单中,该值将被转换回字符串,这基本上就是您在上面一行中所做的。相反,您需要将解析后的值存储在变量中,然后在验证中使用这些变量

var nSmokes = parseInt(document.myForm.nSmokes.value);
然后像这样使用它:

if (nSmokes < 0) {
if(nSmokes<0){

此外,您的逻辑中有一个错误,因为您使用的是
&&
(AND)运算符,而不是
|
(OR)运算符,这会更有意义。

这一行不会像您认为的那样工作:

document.myForm.nSmokes.value = parseInt(document.myForm.nSmokes.value);
一旦您再次将值放回表单中,该值将被转换回字符串,这基本上就是您在上面一行中所做的。相反,您需要将解析后的值存储在变量中,然后在验证中使用这些变量

var nSmokes = parseInt(document.myForm.nSmokes.value);
然后像这样使用它:

if (nSmokes < 0) {
if(nSmokes<0){

此外,您的逻辑中有一个错误,因为您使用的是
&&
(AND)运算符,而不是
|
(OR)运算符,后者更有意义。

document.myForm.nPrice.value=parseFloat(document.myForm.nPrice.value);document.myForm.nAmount.value=parseInt(document.myForm.nAmount.value);document.myForm.nSmokes.value=parseInt(document.myForm.nSmokes.value);
在您的代码中没有任何作用。DOM元素不包含数据类型-您需要将其放置在Javascript变量中,它才能包含类型。
document.myForm.nPrice.value=parseFloat(document.myForm.nPrice.value);document.myForm.nAmount.value=parseInt(document.myForm.nAmount.value);document.myForm.nSmokes.value=parseInt(document.myForm.nSmokes.value);
在你的代码中什么都不做。DOM元素不包含数据类型-你需要将它放在Javascript变量中,它才能包含类型。感谢你对这个问题的兴趣。我实际上是在说明:其中一包的价格是一个大于零小于20的浮点数,而一包中的香烟数是一个整数mber大于0且小于40。我的错误必须小于0且大于40。因此,例如,如果用户输入50,它将得到一个错误。如果if表达的是相反的,它将验证它,不是吗?感谢大家对这个问题的兴趣。我实际上说明了:如果一个包的价格是一个浮点数larger小于0小于20,一包香烟的数量是一个大于0小于40的整数。要使我的错误生效,它必须小于0大于40。因此,例如,如果用户输入50,它将得到一个错误。如果if表示相反的值,它将验证它,不是吗?对!仍然是编码的新手。I c我明白为什么没有意义了。OR应该可以工作。对!对编码来说还是新手。我明白为什么没有意义了