Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Aug 2011
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts

    How to "not post" if the input is blank

    allright, so with this funtion what happens is that the content of the input gets posted on div with a "ul" in it . the only thing is that is actually posting everything, even when it has nothing, it posts the blank value.

    How could i make it NOT post anything if the input value is blank ? ...

    I tried to add an iff , but its not working... =[

    any Guidance pls?


    Code:
    <script type="text/javascript">
    $(document).ready(function(){
    $("form#postbar_add_post").submit(function() {
    
    var addcontentbox = $('#addcontentbox').attr('value');
    
    $.ajax({
    type: "POST",
    url: "postear.php",
    data:"addcontentbox="+ addcontentbox,
    success: function(){
    $("ul#wall").prepend("<li>"+addcontentbox+"</li>");
    $("ul#wall li:first").fadeIn();
    document.postbar_add_post.addcontentbox.value='';
    }
    });
    return false;
    });
    });
    </script>
    postbar_add_post = form name
    addcontentbox = text input name



    or ... should I try from the php file ? ...


    Any sugestions/helps/guidance/ideas are kindly appretiated

  • #2
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    where is your if clause

    you may need to validate that text box before calling ajax function

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,561
    Thanks
    80
    Thanked 4,496 Times in 4,460 Posts
    I don't use jQuery, but I think all you need is as simple as this:
    Code:
    $(document).ready(function(){
        $("form#postbar_add_post").submit(function() {
            var addcontentbox = $('#addcontentbox').attr('value');
    
            if ( addcontentbox.replace(/\s/g,"") == "" ) return false; // don't call ajax if blank
    
           $.ajax({
                ... rest same ...
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    New Coder
    Join Date
    Aug 2011
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    I don't use jQuery, but I think all you need is as simple as this:
    Code:
    $(document).ready(function(){
        $("form#postbar_add_post").submit(function() {
            var addcontentbox = $('#addcontentbox').attr('value');
    
            if ( addcontentbox.replace(/\s/g,"") == "" ) return false; // don't call ajax if blank
    
           $.ajax({
                ... rest same ...
    That was exactly what i was missing !! think u so much ! =D .

    One question tought, what does the (/\s/g,"") means? more specificly, the /\ ...

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,561
    Thanks
    80
    Thanked 4,496 Times in 4,460 Posts
    Code:
    addcontentbox.replace(/\s/g,"")
    /.../ designates a regular expression.
    \s means "any whitespace character" (space, line break, tab)
    /\s/ means "find any whitespace character"
    /\s/g means "and find *ALL* of them" (g == "global")

    SO that says "replace all spaces, line breaks, and tabs with the blank string".

    In other words, if the person type in nothing but a bunch of spaces, strip them all out, thus finding out there is no real content in the value. And then we test the value (using == "" ) and, if it's blank, don't call AJAX.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •