html form required attribute field

96 views Asked by At

My form is not honoring the required attribute of the "comments" field. I want it so that when the user submits the form, it shows "please fill the field", but instead the user can submit the form straight away.

<form class="form-part" method="post" action="contact_form.php" name="contactform" id="contactform" onSubmit="return checkForm1();">
    <input id="name" type="text" name="name" size="30" title="Name" required>
    <input id="email" type="text" name="email" size="30" title="Email" required>
    <textarea rows="3" id="comments" name="comments" cols="40" title="Tell us what you think!" required></textarea>   
    <input type="submit"  name="Submit" alt="Send">
</form>
2

There are 2 answers

2
Carsten Massmann On

A very simple check function might be:

function checkForm1(frm){
  if (frm.comments.value==''){
    alert('Please leave a comment.' );
    return false;
  else return true;
}

For this to work you need to augment your form tag to:

<form ... onsubmitt="return checkForm1(this)">
0
mahesh On

In your code Remove required from Tag

<form class="form-part" method="post" action="contact_form.php"  name="contactform" id="contactform">
<input id="email" type="text" name="email" size="30" title="Email" required>

<textarea rows="3" id="comments" name="comments" cols="40" title="Tell us what you think!" required></textarea>

  <input type="submit"  name="Submit" alt="Send" onSubmit="return checkForm1();">
</form>

and now run. and tell me form show or not.