Use JavaScript To Require Data To Be Entered Into A Form Field Before It Can Be Submitted
Posted by alex in Code Snippets at 10:27 PMI 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:
- Add JavaScript: First in the head portion of your page add the below JavaScript.
JavaScript To Add To Head Section Of Page:
<script type='text/JavaScript'> function checkrequired(which){ var pass=true if (document.images){ for (i=0;i<which.length;i++){ var tempobj=which.elements[i] if (tempobj.alt.substring(0,8)=="required"){ if (((tempobj.type=="text"||tempobj.type=="textarea")&&tempobj.value=='')||(tempobj.type.toString().charAt(0)=="s"&&tempobj. selectedIndex==-1)){ pass=false break } } } } if (!pass){ alert("One or more of the required elements are not completed. Please complete them, then submit again!") return false } else return true } </script> <script type='text/JavaScript' src='/content/js/scw.js'></script> <script type="text/javascript"> $().ready(function(){ $("#nav_profile").attr('className',"on"); $("#cancel").bind('click', function(){history.go(-1);}) }); </script> - 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:
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:
<form name="" onSubmit="return checkrequired(this)" action="{% url edit_user request.user.id %}" method="post"> - 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:
<input id="id_date" alt="required" name="date" onclick='scwShow(this,event);' value='{{ user.dat 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.
|
|
|
|




Entries (RSS)
Excellent work. Thanks
[Reply]
alex Reply:
April 21st, 2010 at 2:27 PM
Hello mutuelle,
No problem. Thanks for taking the time to leave feedback.
Thanks.
alex
[Reply]
Thanks for the post, enjoyed it.
[Reply]
alex Reply:
May 4th, 2010 at 2:34 PM
Hello custom,
No problem at all. Thanks for taking the time to leave feedback.
Thanks.
alex
[Reply]
Hello Webmaster, superb article, I’m going to post some of the information on my blog – I hope you don’t mind if I post a url in return?
[Reply]
alex Reply:
June 4th, 2010 at 9:51 AM
Hello Percy,
Thanks much. We don’t mind at all if you provide credit via URL to our site. Thanks for the feedback and for spreading the information to others.
Thanks.
alex
[Reply]
i wana learn JavaScript. i will surely buy these books
[Reply]
alex Reply:
June 23rd, 2010 at 10:01 AM
Hello mutuelle,
Sounds good. I am by no means an expert in JavaScript but those books looked like they could be helpful in learning JavaScript.
Thanks.
alex
[Reply]
m0dFsq Great tips!!!
[Reply]
alex Reply:
August 26th, 2011 at 8:05 PM
Hello someuser,
Thanks for leaving feedback.
Thanks.
alex
[Reply]
hate the captcha
[Reply]
alex Reply:
August 26th, 2011 at 6:18 PM
Hello zakonczone,
Hate is a strong word…
Thanks.
alex
[Reply]
mutuelle fonctionnaire mutuelle fonctionnaire mutuelle mutuelle fonctionnaire mutuelle
[Reply]