• Home »
  • »
  • Use JavaScript To Require Data To Be Entered Into A Form Field Before It Can Be Submitted

Use JavaScript To Require Data To Be Entered Into A Form Field Before It Can Be Submitted

I needed a simple way to require data in a form field before it could be submitted. There are chunks of JavaScript code all of the Internet that allow you to do this easily so I selected one and added it to my form with ease. One small issue I had was it required that the name attribute be modified for it to work which caused an issue with the application I was adding it to. The issue was the name attribute was modified thus it would not save the data to the proper database table column. Below I explain how to add this bit of JavaScript to your form and modify your form for it to function properly.

The initial JavaScript I located used the input element name attribute to trigger the requirement message to pop up. This did not work for me and I imagine it would not work for others who are submitting data to an application because applications typically use the name attribute as well. The below JavaScript was slightly modified to use the input element alt attribute instead.

Require Data Be Entered In A Form Field Before It Is Submitted:

  1. Add JavaScript: First in the head portion of your page add the below JavaScript.

    JavaScript To Add To Head Section Of Page:


    1. <script type='text/JavaScript'>
    2.         function checkrequired(which){
    3.         var pass=true
    4.         if (document.images){
    5.         for (i=0;i<which.length;i++){
    6.         var tempobj=which.elements&#91;i&#93;
    7.         if (tempobj.alt.substring(0,8)=="required"){
    8.         if (((tempobj.type=="text"||tempobj.type=="textarea")&amp;&amp;tempobj.value=='')||(tempobj.type.toString().charAt(0)=="s"&amp;&amp;tempobj.
    9. selectedIndex==-1)){
    10.                 pass=false
    11.         break
    12.         }
    13.         }
    14.         }
    15.         }
    16.         if (!pass){
    17.         alert("One or more of the required elements are not completed. Please complete them, then submit again!")
    18.         return false
    19.         }
    20.         else
    21.         return true
    22.         }
    23.         </script>
    24.         <script type='text/JavaScript' src='/content/js/scw.js'></script>
    25.         <script type="text/javascript">
    26.             $().ready(function(){
    27.                 $("#nav_profile").attr('className',"on");
    28.                 $("#cancel").bind('click', function(){history.go(-1);})
    29.             });    
    30.         </script>
  2. Modify Form Element: Now in the form element place the below attribute which will trigger once the form is submitted. Any input field that has “required” as part of the alt tag will require data to be entered into that field.

    JavaScript To Add To Form Element:


    1. onSubmit="return checkrequired(this)"

    The entire form element would look like the below now that this JavaScript has been added.

    Example Form Element With JavaScript Added:


    1. <form name="" onSubmit="return checkrequired(this)" action="{% url edit_user request.user.id %}" method="post">
  3. Modify Input Fields: Once the two steps have been completed you are now ready to select the required input fields and add the necessary trigger. Again the trigger will be the “alt” attribute so any input field that has an alt tag with the word required in it will require data be entered into that form field before it will allow the form to be submitted. You can customize the pop up message by editing the JavaScript in step one.

    Example Input Field With Added Alt Tag:


    1. <input id="id_date" alt="required" name="date" onclick='scwShow(this,event);' value='{{ user.dat
    2. e }}'/>

I would suggest adding the alt tag to one form field to verify everything is functioning before adding to a bunch of fields. That is all you have to do though. If you have a site with many pages you should make sure to only load the above JavaScript to this page so it doesn’t cause extra weight for every page on your web site.

JavaScript: The Good Parts (Paperback)

List Price: $29.99 USD
New From: $14.54 USD In Stock
Used from: $12.98 USD In Stock

JavaScript: The Definitive Guide (Paperback)

List Price: $49.99
New From: $21.02 USD In Stock
Used from: $0.14 USD In Stock