0

Hi I have a form where a user can enter one or more books into a DB. Whenever a user enters one book and forgets to enter the title, a JavaScript alert comes and alerts him to enter a title. Now if he has two or more books and he forgets to enter the title, the alert doesn't show up.

This is my JavaScript function.

function validateForm() 
{ 
 var a=document.forms["submit_books"]["title"].value; 
  if (a==null || a=="") 
    { 
    alert("Please enter a Title"); 
    return false; 
    }


 var status = false;     
 var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
      if (document.submit_books.email.value.search(emailRegEx) == -1) {
           alert("Please enter a valid email address.");
           return false;
      }
}

And Here is my PHP code

<form method="post" name="submit_books" onsubmit="return validateForm()" action="<?php echo $_SERVER['PHP_SELF']; ?>">
                <?php for ($i=1; $i<$num_of_books + 1; $i++){
                    echo "<strong>Book # $i</strong><br><br>";
                    ?>


                    <label for="title">*Title</label>: <input type="text" id="title" size="60" name="title[]" autocomplete="off"/><br><br>

              <?php }?>
<input type="submit" name="submit" value="Submit Books">
</form>




I even tried putting the PHP array into a JavaScript one.

     <?
$js_array = json_encode($title);
echo "var title = ". $js_array . ";\n";
?>
var index = 1;
if( index < title.length)
{
    alert("Please enter a Title"); 
    return false; 
} 

There must be an easier way doing this

Peter O.
  • 32,158
  • 14
  • 82
  • 96
user1536365
  • 25
  • 1
  • 7
  • If you just need to check if it is empty, why not putting the .length property into a javascript variable? – Hanlet Escaño Apr 18 '13 at 03:10
  • Your regex email is too slim, you should use the one in [this answer](http://stackoverflow.com/a/719543/731947) – CSᵠ Apr 18 '13 at 03:14

3 Answers3

1

You should be doing

var index = 1;
if( index > title.length )
{
    alert("Please enter a Title"); 
    return false; 
}

Since there is no record if title.length = 0, that is, if 1 > 0 then there is no title.

You can also check

 if( title.length === 0 )
Jonast92
  • 4,964
  • 1
  • 18
  • 32
0

You can do this like:

 <?
echo "var title_length = ". count($title) . ";\n";
?>
var index = 1;
if( index > title_length)
{
    alert("Please enter a Title"); 
    return false; 
} 
Supriti Panda
  • 1,281
  • 1
  • 11
  • 12
0

Try to use inside html form

<label> 
<span>Book Title: (required)</span> 
<input name="book" type="text" placeholder="Please enter your books title" required autofocus> 
</label>

Then use javascript to validate

(function() {

    // Create input element for testing
    var inputs = document.createElement('input');

    // Create the supports object
    var supports = {};

    supports.autofocus   = 'autofocus' in inputs;
    supports.required    = 'required' in inputs;
    supports.placeholder = 'placeholder' in inputs;

    // Fallback for autofocus attribute
    if(!supports.autofocus) {

    }

    // Fallback for required attribute
    if(!supports.required) {

    }

    // Fallback for placeholder attribute
    if(!supports.placeholder) {

    }

    // Change text inside send button on submit
    var send = document.getElementById('submit');
    if(send) {
        send.onclick = function () {
            this.innerHTML = '...Processing';
        }
    }

})();
Doavers
  • 16
  • 6