Home > Web Front-end > JS Tutorial > body text

jquery implements form validation and prevents illegal submissions

PHPz
Release: 2018-10-12 17:35:24
Original
1330 people have browsed it

This article shares with you the code for using jquery to implement form verification and prevent illegal submissions. The idea is very good. I recommend it to my friends for reference.

The code detects the length filled in the textarea. If it is not filled in, it will prompt that it needs to be filled in again. If it is less than 15 characters, it will prompt that it needs to be longer than 15 characters. If it is successful, it will display the filled-in suggestions.

<script type="text/javascript">
//jQuery代码
function confirm()
{
  if($("#advice").val().length == 0)
{
  alert("We can&#39;t see your advice. Maybe you should fill the form first.");
  $("#advice").focus();
  return false;
}
else if($("#advice").val().length <= 15)
{
  alert("Your advice should longer than 15 letters.");
  $("#advice").focus();
  return false;
}
else
{
  alert("Thank you for your advice! You will get out reply in 24 hours for advice:\n"+$("#advice").val());
  return true;
}
}
</script>
<form action="" method="post" onSubmit="return confirm();" >
  <textarea id="advice" rows="20" cols="50"placeholder="Give us some advice?"></textarea>
   <input type="submit"value="Thank you"/>
</form>
Copy after login

>Key points

1. There must be onSubmit="return confirm();" The word return is indispensable.
2. Complete web page structure by yourself.

The above is the entire content of this article, I hope you all like it.

For more related tutorials, please visit JavaScript Tutorial

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template