...

View Full Version : How to "not post" if the input is blank



Nightcrawler89
08-19-2011, 09:38 PM
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?



<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

jovadviser
08-20-2011, 12:58 AM
where is your if clause

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

Old Pedant
08-20-2011, 10:05 PM
I don't use jQuery, but I think all you need is as simple as this:


$(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 ...

Nightcrawler89
08-20-2011, 10:27 PM
I don't use jQuery, but I think all you need is as simple as this:


$(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 /\ ...

Old Pedant
08-20-2011, 10:54 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum