title: JavaScript form validation - to check for an empty textbox - using Python and CGI
body: <p>I am developing a web application using Python and CGI. For form validation, I intend to use JavaScript. Here is the scenario -
  There is a textbox where in user enters the name of the project and clicks the submit button.
  I want to catch if nothing is entered in the textbox and throw an alert.</p>
  
  <p>For the JavaScript function, I have used parameters, project_form (for the form) and project_name for the textbox. But what I am not clear about is where to put this function. As of now, I am trying to put it in the form tag itself. But it is not working. I somehow think that I have to pass the project_form and project_name parameters to the function and due to th e code structure, it is not possible for me to put the function in the 'head' section. I am pasting my code here. Thanks for the help.</p>
  
  <pre><code>&lt;form action="create_project.py" method="POST" name="project_form"&gt;
    &lt;input type="text" name="project_name" size="40" onclick="javascript:document.project_form.submit.disabled=false"
    id="acpro_inp0"&gt;
    &lt;br&gt;
    &lt;script type="text/javascript"&gt;
      function ValidateTextbox(project_form, project_name) {
        var tb_value = project_form.project_name.value;
        if (tb_value == null || tb_value.trim() == "") {
          alert("Please enter something");
          project_form.project_name.focus();
          return false;
        }
        return true;
      }
    &lt;/script&gt;
    return ValidateTextbox('project_form','project_name')
    &lt;p&gt;
    &lt;/p&gt;
    &lt;input type="submit" name="submit" value="Submit" onsubmit="return ValidateTextbox('project_form','project_name')"
    disabled=""&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Here is the related Python code -</p>
  
  <pre><code>import yate
  import jshelper
  
  print yate.start_response()
  print yate.include_header("Create a new project.")
  #Section to create a new project
  print yate.start_form("create_project.py","project_form")
  print yate.text_box("project_name",form_name="project_form")
  jshelper.JSTextBoxValidate("project_form","project_name")
  print yate.end_form("Submit",is_disabled = 'True', onsubmit_callback = "return ValidateTextbox('project_form','project_name')")
  print yate.include_footer(({"Home": "../index.html"}))
  </code></pre>
  
  <p>Here is another piece of Python code, where I try to create the JS function on the fly.</p>
  
  <pre><code>def JSTextBoxValidate(form_name,tb_name):
      print '&lt;script type="text/javascript"&gt;'
      print 'function ValidateTextbox' + '(' + form_name + ',' + tb_name + ')'
      print '{' 
      print 'var tb_value = ' + form_name + '.' + tb_name + '.' + 'value;' 
      print 'if (tb_value==null || tb_value.trim()=="")'
      print '{' 
      print 'alert("Please enter something");'
      print form_name + '.' + tb_name + '.' + 'focus();' 
      print 'return false;'
      print '}' 
      print 'return true;'
      print '}'
      print '&lt;/script&gt;'
  </code></pre>
  
  <p>Somehow I have a feeling that I am probably not on the right track in this process and there has to be a better way. So I will be thankful for helping me to get this thing to work.
  Note - The package yate is from Head First Python and I am using and extending the template from that code. I have put similar note in my project's documentation. This is my personal project.</p>