请检查此javascript代码中的错误 [英] please check the error in this javascript code

查看:89
本文介绍了请检查此javascript代码中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if (y.charAt(0)!="9"||y.charAt(0)!="8"||y.charAt(0)!="7"||y.charAt(0)!="6")
           {
                alert("it should  with 9 or 8 or 7 or 6");
                theForm.txtPhone.focus();
                return false
           }


我正在编写此代码来验证电话号码
但仅适用于以9开头的数字.

我认为我或我的操作有误.


我的完整代码是




i am writing this code for validating phone number
but is working only for number start with 9 only.

i think my or operation is wrong .


my full code is



var y = theForm.txtPhone.value;
       if (theForm.txtPhone.value == "")
       {
       alert("Enter phone number");
              theForm.txtPhone.focus();
              return false;
       }
       if(isNaN(y)||y.indexOf(" ")!=-1)
           {
              alert("Enter numeric value");
              theForm.txtPhone.focus();
              return false;
           }
           if (y.length != 10)
           {
                alert("enter 10 characters");
                theForm.txtPhone.focus();
                return false;
           }
           if (y.charAt(0)!="9"||y.charAt(0)!="8"||y.charAt(0)!="7"||y.charAt(0)!="6")
           {
                alert("it should  with 9 or 8 or 7 or 6");
                theForm.txtPhone.focus();
                return false
           }



请帮助我

vara prasad



please help me

vara prasad

推荐答案

使用&&代替

if (y.charAt(0)!="9" && y.charAt(0)!="8" && y.charAt(0)!="7" && y.charAt(0)!="6")
Use && instead

if (y.charAt(0)!="9" && y.charAt(0)!="8" && y.charAt(0)!="7" && y.charAt(0)!="6")


请再次检查逻辑,我相信您应该使用"AND"运算符而不是"OR".



如果要保留"OR"运算符,请使用"=="代替!=".
Please check the logic again, I believe you should use the "AND" operator instead of "OR".

OR

Use "==" instead of "!=" if you want to keep "OR" operator.


在这种情况下,您需要检查"And"条件而不是"OR"
如果要使用"OR",则需要检查相反的条件,如下所示

如果(y.charAt(0)=="0" || y.charAt(0)!="1" || y.charAt(0)!="2" || y.charAt(0)!="3"|| y.charAt(0)!=" 4"|| y.charAt(0)!=" 5)
in this case you need to check with "And" condition instead of "OR"
if you want to use "OR" then you need to check the opposite condition like below

if (y.charAt(0)=="0"||y.charAt(0)!="1"||y.charAt(0)!="2"||y.charAt(0)!="3"||y.charAt(0)!="4"||y.charAt(0)!="5")


这篇关于请检查此javascript代码中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆