0

code:

// if (image.substring(0,7) != "http://" && image.substring(image.length-4) != ".jpg" || ".png")
if (image.substring(0,7) != "http://" && image.substring(image.length-4) != ".jpg" && image.substring(image.length-4) != ".png")
    {
            // do stuff
    }

context: Trying to do an if-statement in JavaScript which uses the && and || operators but it's not working out... Might be due to the fact of how JavaScript returns true-like and false-like values.

EDIT:
full code:

function validateContent(title, rating, link, image, desc)
{
    var flag = true;

    // add more if-statements
    if (title == "")
    {
        $("#inputTitle").css("background-color", "red");
        flag = false;
    }
    if (rating == 0)
    {
        $("#selectRating").css("background-color", "red");
        flag = false;
    }
    if (link.substring(0,26) != "http://www.imdb.com/title/")
    {
        $("#inputLink").css("background-color", "red");
        flag = false;
    }
    // if (image.substring(0,7) != "http://" && image.substring(image.length-4) != ".jpg" || ".png")
    //if (image.substring(0,7) != "http://" && image.substring(image.length-4) != ".jpg" && image.substring(image.length-4) != ".png")
    //if ((image.substring(0,7) != "http://") && (image.substring(image.length-4) != ".jpg") && (image.substring(image.length-4) != ".png"))
    //if (image.substring(0,7) != "http://" && (image.substring(image.length-4) != ".jpg" || image.substr(image.length-4) !=  ".png"))
    //if (/^http:\/\/.*\.(jpg|png)$/.test(image))
    if ((image.substring(0,7) != "http://") && ((image.substring(image.length-4) == true) != ".jpg") && ((image.substring(image.length-4) == true) != ".png"))
    {
            $("#inputImage").css("background-color", "red");
            flag = false;
    }
    return flag;
}

function addContent()
{
    var title = $("#inputTitle").val();
    var rating = $("#selectRating option:selected").index();
    var link = $("#inputLink").val();
    var image = $("#inputImage").val();
    var desc = $("#textareaDescription").val();

    if (validateContent(title, rating, link, image, desc))
    {
        document.forms.formFilmerPHP.submit();
    }
}

function disableOption(pos)
{
    $("#selectRating option:eq(" + pos + ")").prop("disabled", true);
}

function validateInputs(inputType)
{
    // add more cases
    switch(inputType)
    {
        case "title":
            var title = $("#inputTitle").val();
            if (title != "")
                $("#inputTitle").css("background-color", "white");
            break;
        case "rating":
            var rating = $("#selectRating option:selected").index();
            if (rating != 0)
                $("#selectRating").css("background-color", "white");
            break;
        case "link":
            var link = $("#inputLink").val();
            if (link.substring(0,26) == "http://www.imdb.com/title/")
                $("#inputLink").css("background-color", "white");
            break;
    }
}

function preventDefault()
{
    $("#btnAddContent").click(function(event) {event.preventDefault();});
}

function addEventListeners()    // QUESTION: when to use anonymous functions and when not to when adding eventlisteners in order to safely attach functions without invoking them?
{
    // misc eventlisteners
    $("#selectRating").on("focus", function() {disableOption(0);});

    // real-time polling of invalid input correction
    $("#inputTitle").on("input", function() {validateInputs("title");});                    // QUESTION:    takes some time before this fires, how to make it fire more quickly?
                                                                                                                            // ANSWER:      use the "oninput" event, previously used onkeydown
    $("#selectRating").on("change", function() {validateInputs("rating");});
    $("#inputLink").on("input", function() {validateInputs("link");});

    // main eventlisteners
    $("#btnAddContent").on("click", function() {addContent();});
}

function init()
{
    preventDefault();
    addEventListeners();
}

/* method used to test during development */
function devtest()
{

}

$(document).ready(init);

EDIT:

    var a = image.substring(0,7);
    var b = image.substring(image.length-4);
    alert(a);
    alert(b);
    if(a != "http://" && b != ".jpg" && b != ".png")
    {
            $("#inputImage").css("background-color", "red");    // <-- not being executed
            flag = false;
    }
    return flag;

can't get the $("#inputImage").css("background-color", "red"); statement to execute even though the condition should be evaluated to true.

16
  • 2
    At least, assign the value to the image variable so that we can help you understand why this is not working. Also, I don't see any || operator here. Commented Nov 16, 2013 at 22:46
  • You should make working the conditions one by one. And try to make compound conditions if they are working already. Commented Nov 16, 2013 at 22:48
  • 3
    Please explain what the code is supposed to do/which problem you are trying to solve. Also what exactly does not work out? What input do you have and what output do you expect? That it is an if statement I can see for myself. Commented Nov 16, 2013 at 22:50
  • 1
    I think you need to explain in more detail what you need. That regex will check for images that start with http:// and end in .jpg or .png. Isn't that what you want? Commented Nov 16, 2013 at 23:01
  • 2
    More important than your wall of code is an explanation of what you are trying to do. Commented Nov 16, 2013 at 23:02

3 Answers 3

2

The below code will be evaluated as true when the first part of the string isn't http:// and the strings last four characters are neither .jpg nor .png

if (image.substring(0,7) != "http://" && 
    image.substring(image.length-4) != ".jpg" && 
    image.substring(image.length-4) != ".png")
Sign up to request clarification or add additional context in comments.

8 Comments

@DaggNabbit - The || operator
@DaggNabbit - I assumed (based on the question title) he meant the javascript that was commented out.
didn't work, evaluates to true when (image.substring(0,7) != "http://") is satisfied.
@Ryuji - I think it strange that you ask a question about the || operator when you're not using one in the actual code? I updated my question though.
@KristerAndersson I did have || originally but it didn't work as expected so I uncommented it, just because it's uncommented doesn't mean that the || operator is irrelevant to the question. I'll check your updated answer. EDIT: still doesn't work.
|
0

Ok, after a night of good sleep, here's the proper solution:

    var imagebegin = image.substring(0,7);
    var imageend = image.substring(image.length-4);
    if(imagebegin != "http://" || (imageend != ".jpg" && imageend != ".png"))
    {
            $("#inputImage").css("background-color", "red");
            flag = false;
    }
    return flag;

The validation will occur only if the beginning of the string is "http://" and the end is either ".jpg" or ".png", if either of these conditions aren't met then the if-statement will evaluate to true and the statements within will be executed (including setting the flag to false).

Comments

-1

sorry i didnt read your answer proper but i think i gave the right hint anyway

if (image.substring(0,7) != "http://" && image.substring(image.length-4) != ".jpg" && (image.substring(image.length-4) != ".png" || image.substring(image.length-4) != 'or'))

1 Comment

didn't work, evaluates to true when (image.substring(0,7) != "http://") is satisfied.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.