Jump to content
Diana Madeira

Validar uma form

Recommended Posts

Diana Madeira

Olá 

Tenho seguinte código que me parece estar bem mas no entanto não está a funcionar a 100%. por exemplo se não colocar o ISBN diz que não coloquei mas os outros campos não está a reconhecer.

<html>
    <head>
        <title> Add new record!</title>
        <script type ="text/javascript" >
  
           function return validate()
    {
      
         if( document.myForm.ISBN.value == "" )
         {
            alert( "Please provide ISBN!" );
            document.myForm.ISBN.focus() ;
            return false;
         }
         
         else if( document.myForm.AuthorsName.value == "" )
         {
            alert( "Please provide Author's name!" );
            document.myForm.Authorsname.focus() ;
            return false;
         }
   
            else if( document.myForm.Title.value == "" )
         {
            alert( "Please provide a Title!" );
            document.myForm.Title.focus() ;
            return false;
         }
            else if( document.myForm.edition.value == "" )
         {
            alert( "Please provide edition!" );
            isNaN( document.myForm.edition.value )
            return false;
         }
          else if( document.myForm.year.value == "" )
         {
            alert( "Please provide year!" );
            isNaN( document.myForm.year.value )
            return false;
         }
           else if( document.myForm.category.value == "" )
         {
            alert( "Please provide category!" );
            document.myForm.category.focus() ;
            return false;
         }
         
         else if( document.myForm.publisher.value == "" )
         {
            alert( "Please provide publisher!" );
            document.myForm.publisher.focus() ;
            return false;
         }
          
        else if( document.myForm.quantityinstock.value == "" )
         {
            alert( "Please provide quantity!" );
            isNaN( document.myForm.quantityinstock.value )
            return false;
         }
          else if( document.myForm.price.value == "" )
         {
            alert( "Please provide price!" );
            isNaN( document.myForm.price.value )
            return false;
         }
          
         return( true );
      }
            
        
        
        </script>
    </head>
    <body>
        <?php include 'addDataAdmin.php';?>
        <form name="myForm" action="addDataAdmin.php" method="POST" onsubmit="return validate()" enctype="multipart/form-data" autocomplete="off" >
            <p>ISBN</p> <input type="text" name="ISBN">
            <p>Author's Name</p> <input type="text" name="Authorsname">
            <p>Title</p> <input type="text" name="Title">
            <p>Edition</p> <input type="text" name="edition">
            <p>Year</p> <input type="text" name="year">

            <p>Category</p>
            <select name="category" size="1">
                <option value="computing">Computing</option>
                <option value="Romance">Romance</option>
                <option value="Fiction">Fiction</option>
                <option value="Non-Fiction">Non-Fiction</option>
            </select>
            <br />

            <p>Publisher</p> <input type="text" name="publisher">
            <p>Quantity-in-stock</p> <input type="text" name="quantityinstock">
            <p>Price</p> <input type="text" name="price">
            <input type="file" name="fileToUpload" id="fileToUpload">
            <input type="submit" value="Upload Image" name="submit" formaction="/upload.php">
            <input type="submit" value="Send" name="send">
            <input type="reset" value="Clear">
        </form>
    </body>
</html>

tem como alguém ajudar sff?

Obrigado

Share this post


Link to post
Share on other sites
Marlyn Filipe

Olá Diana.

Só pretende verificar um campo de cada vez, certo?  Porque se quiser uma mensagem por cada campo vazio basta tirar os elses. Uma estrutura de if () ... if (), em vez de if () ... else if ().

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.