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.


  • Count per Day

    • 4Visitors today:
    • 40Visitors per day:
    • 0Visitors currently online:
  • ScrapBook Software - MyMemoriesSuite
  • Latest Pins on Pinterest

    • Blue Carolina Starburst Quilt {Free Pattern}

    • Well Texas Tumbleweeds got finished.  If you missed that, find it here.  So it’s time for a new UFO.  Earlier I had pulled Fair and Square but after I got the string triangles done, I left it…

    • 719 Likes, 25 Comments - David (@scfdavid) on Instagram: “Here’s my Blockheads block for Week 45 - Double Dutch - from my friend, Jo Morton! Thanks, Jo! All…”

    • Quilt patriotic Hidden Glorry reserved for Carrie by marytequilts

    • Tutorial - How to Get Perfect Centers

    • How to sew and trim half rectangle triangles (HRTs) - Kirsty of Bonjour Quilts

    • Center Point designed by Elise Lea for Robert Kaufman. Features Chalk and Charcoal by Jennifer Sampou, shipping to stores May 2018. Roll up friendly. FREE pattern available for download from robertkaufman.com May 2018. #FREEatrobertkaufmandotcom

    • A modern quilt blog focused on practical tutorials and quilting tips. Sharing traditionally pieced quilt patterns and lots of free quilt alongs.