最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

NOT EQUAL TO not working in javascript - Stack Overflow

programmeradmin6浏览0评论

When I am using this:

case "vic":
    if ((bPostcodeSubstring == 3) || (bPostcodeSubstring == 8)){
        return true;
    }
    else{
        errMsg += "Enter A Valid Postcode.";
        result = false;             
    }
break;

It is working fine. But when I am using this:

case "vic":
    if ((bPostcodeSubstring != 3) || (bPostcodeSubstring != 8)){
        errMsg += "Enter A Valid Postcode.";
        result = false;             
    }
break;

It is not working at all. What's the problem?

When I am using this:

case "vic":
    if ((bPostcodeSubstring == 3) || (bPostcodeSubstring == 8)){
        return true;
    }
    else{
        errMsg += "Enter A Valid Postcode.";
        result = false;             
    }
break;

It is working fine. But when I am using this:

case "vic":
    if ((bPostcodeSubstring != 3) || (bPostcodeSubstring != 8)){
        errMsg += "Enter A Valid Postcode.";
        result = false;             
    }
break;

It is not working at all. What's the problem?

Share Improve this question edited Oct 21, 2015 at 4:42 Abhas Tandon 1,88916 silver badges26 bronze badges asked Apr 21, 2014 at 9:35 HelmBurgerHelmBurger 1,3086 gold badges18 silver badges42 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 6
!((bPostcodeSubstring == 3) || (bPostcodeSubstring == 8))

is NOT same as

(bPostcodeSubstring != 3) || (bPostcodeSubstring != 8)

it should be

(bPostcodeSubstring != 3) && (bPostcodeSubstring != 8)

demorgan's law

If you want both 3 and 8 unacceptable for this variable, then you have to use &&.

if ((bPostcodeSubstring != 3) && (bPostcodeSubstring != 8)){
    errMsg += "Enter A Valid Postcode.";
    result = false;             
}

The or is resolving always to true, consider if bPostcodeSubstring is 3 then first condition fails but second gives true. If you want to exclude 3 and 8 then use not outside of both or condition

if (!(bPostcodeSubstring == 3 || bPostcodeSubstring == 8)){

OR use and && instead of || If you want to exclude 3 and 8 then use not outside of both or condition

if ((bPostcodeSubstring != 3) && (bPostcodeSubstring != 8)){

You should use && insead of || in your second if.

Indeed, the opposite of :

(bPostcodeSubstring == 3) || (bPostcodeSubstring == 8)

Is :

(bPostcodeSubstring != 3) && (bPostcodeSubstring != 8)
发布评论

评论列表(0)

  1. 暂无评论