0

I have 2 text boxes with default value 0 and a submit button.Before submitting I am calling the following javascript function onSubmit="return(validate_myfrm());".I am not able to validate text_1 with value 0.But for text_2 the default value 0 is validated.

function validate_myfrm()
{

   var ans;
   if(document.myfrm.txt_1.value=="" ||document.myfrm.txt_1.value==0)
      {
              ans=confirm("Do you still want to continue with value 0 for text 1?");
              if(ans== true)
                  {
                     document.myfrm.txt_1.value=0;
                     document.myfrm.txt_2.focus();
                  }
              else
                  {
                     document.myfrm.txt_1.focus();
                  }
                 return false;
    }

   if(document.myfrm.txt_2.value=="" ||document.myfrm.txt_2.value==0)
            {
                ans=confirm("Do you still want to continue with value 0 for text 2?");
                if(ans== true)
                  {
                     document.myfrm.txt_2.value="0";
                     return true;
                  }
              else
                  {
                       document.myfrm.txt_2.focus();
                  }
                  return false;
            }
     return true;

}

If i return true; after document.myfrm.txt_2.focus(); the page is redirected to next page without confirming the values for txt_2.Pls help

4

3 回答 3

0

document.myfrm.txt_bb1.value==0

应该是:document.myfrm.txt_1.value==0

还:

设置返回后,函数将结束并返回函数中设置的 false 或 true。

第一个如果根据您的条件值为“”或 0 时将始终返回 false。因为它返回 false,所以函数将结束并返回 false。

编辑:

这是一个简单的验证示例:

<form id="myfrm" name="myfrm" onSubmit="return(validate_myfrm());">
    <input type="text" name="txt_1" value="" />
    <input type="text" name="txt_2" value="" />
    <input type="submit" value="submit" />
</form>
<span id="result"></span>

<script>

function validate_myfrm()
{
   if(document.myfrm.txt_1.value!="" && document.myfrm.txt_2.value!="")
   {
       return true;    
   } else {
       document.getElementById("result").innerHTML="Error fields cannot be empty";
       return false;
   }
}
</script>

请记住,每当您在函数中使用 return 时,它都会停止函数并返回值集。

于 2013-04-22T09:27:54.373 回答
0

现在使用 if 函数的 Else If Instaed,它将在提交前验证。

 function validate_myfrm()
{

   var ans;
   if(document.myfrm.txt_1.value=="" ||document.myfrm.txt_1.value==0)
      {
              ans=confirm("Do you still want to continue with value 0 for text 1?");
              if(ans== true)
                  {
                     document.myfrm.txt_1.value=0;
                     document.myfrm.txt_2.focus();
                  }
              else
                  {
                     document.myfrm.txt_1.focus();
                     return false;
                  }
    }else if(document.myfrm.txt_2.value=="" ||document.myfrm.txt_2.value==0)
            {
                ans=confirm("Do you still want to continue with value 0 for text 2?");
                if(ans== true)
                  {
                     document.myfrm.txt_2.value="0";
                     return true;
                  }
              else
                  {
                       document.myfrm.txt_2.focus();
                  }
                  return false;
            }
     return true;
    }
}
于 2013-04-22T09:41:54.947 回答
0

你的第一个条件应该是这样的

if(document.myfrm.txt_1.value=="" ||document.myfrm.txt_1.value==0)
 {

 }
于 2013-04-22T09:31:16.850 回答