Web Analytics Made Easy -
StatCounter How to "not post" if the input is blank - CodingForum

Announcement

Collapse
No announcement yet.

How to "not post" if the input is blank

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    where is your if clause

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

    Comment


    • #3
      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 ...
      Be yourself. No one else is as qualified.

      Comment


      • #4
        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 /\ ...

        Comment


        • #5
          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.
          Be yourself. No one else is as qualified.

          Comment

          Working...
          X