title: unable set required attribute to true with javascript
body: <p>I have a form.When I submit that form the require attribute of the text field of that form should be set to true.I am trying this with javascript.But unable get the positive result.My code is </p>
  
  <pre><code>&lt;html&gt;
      &lt;head&gt;
          &lt;script&gt;
          function validate()
          {
              alert("ok");
              document.getElementById("sample").required = true;//method-1
  
        document.getElementById("sample").setAttribute("required","")//method-2
          }
          &lt;/script&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;form method="post" action="" onsubmit="validate()"&gt;
              &lt;input type="text" id="sample"&gt;
              &lt;input type="submit" value="submit"&gt;
          &lt;/form&gt;
      &lt;/body&gt;
   &lt;/html&gt;
  </code></pre>
  
  <p>in both these methods I am unable to set required to true.any help please...</p>