My collection of things.

jKafer on the Net

January 12th, 2018 at 12:34 pm

Javascript – check required fields in form – return to form

PROBLEM: check required fields (more than 2) before adding items to a database – this will help curb bad data entries in your database.

I have a form that uses .ajax to write to the database and then relist all the items added. Like a shopping cart…in a way.  So I split out the validation part – it was simpler this way.

<input type=”button” class=”addtocart_button” value=”Save” title=”Save” onClick=”ckItem()” />

SOLUTION:

When the “Save” button is clicked it runs: ckItem()  –

function ckItem(){
//javascript function loaded in header
   
   $formID = "itemEntry";
   //pulls information from form that was submitted
   $category = document.forms[$formID]['category'].value;
   $genius = document.forms[$formID]['desc1'].value;

   //checks required fields for data  
   if (!$category || !$genius) {
       //if required field(s) are blank, do not add record
      alert("Category & Description are required fields");
      if (!$category) {
         document.forms[$formID]['category'].focus();  //returns cursor to this field
         return;  //stops the function
      } else if (!$genius) {
         document.forms[$formID]['desc1'].focus();  //returns cursor to this field
      }
   } else {
      //if both fields have data, it will now write the data to the database
      saveItem();
   };
}

 

Tags: ,
-

Comments are closed.


  • Pages

  • Latest Pins on Pinterest

    • Follow Me on Pinterest

  • Quilting Projects I Want to Make

    • quilt kits black and gray - Google Search

    • How To Make A Quillow. One of my aunts made me one as a high school graduation gift. I wore it out and miss it!

    • Cute Cathedral Window by Pitter Putter Stitch. I'm really wanting to try the cathedral window quilt block and this one is so so pretty!

    • How about a modern I Spy quilt? This pattern uses a total of 51- 5" I Spy squares in the following colors: ...

    • Usando os presentes fofos !

    • My pdf patterns  are available  for purchase from my online Etsy shop here . Wholesale printed patterns are only available directly from m...

    • Arrowhead Quilt PDF Pattern — Initial K Studio

    • Dimension designed by Nydia Kehnle. Features #konacotton. Pattern available for purchase (nydiakehnle.bigca...).

    • Follow Me on Pinterest