1

I have this javascript function:

function displayMessage() {
    var message = $("#msg").val();

    if (message == "") { 
        alert("You need to enter a message");//alert the user
        return false;
    }

    postData = {
        "message": message,
    };    

    ...

}

What am hoping this achieves is, if the input field is empty, display the alert and remain in the function.If it isn't then continue. My submit button is linked to another page but this page is displayed anyways regardless of what happens in the if statement.

This is the form code:

<form id="post" action="http://localhost:8080/uploadNewMessage" method="post">
    <fieldset>
        <div data-role="fieldcontain">
            <label for="msg" class="input">Message:</label>
            <input type="text" name="msg" id="msg" size="10"/>
        </div>
        <a href="profile-page.html" id="submit" data-role="button" data- theme="b">Submit</a>
    </fieldset>
</form>

and the full javascript code just incase:

$(document).ready(function() {
    // 1. The Registration button
    $("#submit").bind('click', function(event) {

        displayMessage();

    });

});

function doPostRequest(postData) {

    $.ajax({

        type: "POST",
        url: URL,
        dataType: "json",
        data: postData

    });
}

function displayMessage() {
    var message = $("#msg").val();

    if (message == "") { 
        alert("You need to enter a message");//alert the user
        return false;
    }

    postData = {
        "message": message,
    };    

    ...
    doPostRequest(postData);
}
2
  • action=<...> what kind of language is this? Commented Jun 2, 2014 at 1:49
  • 2
    It's my native language. :D Pardon me, let me edit that,i was just trying to leave that part out. Commented Jun 2, 2014 at 1:50

3 Answers 3

2

You may try something like this:

$("#submit").bind('click', function(event) {
    var message = $.trim($("#msg").val());
    if(!message.length) {
        alert("You need to enter a message");
        return false;
    }
    else {
        event.preventDefault();
        doPostRequest({"message":message});
    }
});
Sign up to request clarification or add additional context in comments.

5 Comments

why is the return false; needed?
I have tried this, but somehow i am still being redirected to the profile page
Then you have other JS errors, check for the errors on browser's console and fix them.
@OjonugwaOchalifu have you tried all the suggested solutions? Have you set the url: URL, -> URL ?
Am trying them all out now.
1

demo

$(function() {
    $("#submit").on('click', function(event) {
        event.preventDefault(); // prevent default anchor behavior
        displayMessage();
    });
});

and also:

function displayMessage() { 
    var message = $.trim( $("#msg").val() ); // trim whitespaces
    if (message === "") { 
        alert("You need to enter a message");
    }else{                              // Use the else statement
      doPostRequest({
        "message" : message
      });   
    }
}

1 Comment

When i try this with event.preventDefault(), the button changes to another color and the message is posted to GAE but the page doesn't change.
0

The event variable that is passed via your click event handler contains a function named preventDefault. If you don't want the form to submit, call this (event.preventDefault()). This will prevent the submit button from submitting the form.

2 Comments

Objects are variables too.
If I create a new object and assign it to a variable, is that variable no longer a variable?

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.