简体   繁体   中英

html form required attribute field

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>

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)">

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM