JS for form validation

Source: Internet
Author: User

There are two ways to form validation:

First, onsubmit verification:

<script language= "JavaScript" >    function CheckPost  ()  {             if  (addform.user.value ==  "")  {                     alert ("Please fill in the user name!") ");                         addform.username.focus ();                         return  false;                     }                 if  (addform.title.value.length < 5)  {                     alert ("title cannot be less than 5 characters!") ");                         addform.title.focus ();                         return  false;                     }                 return true;            }     </script><form action= "test.php"  method= "post"  name= "AddForm"   Onsubmit= "Return checkpost ();" >    <p> User: <input type= "text"  size= "ten"  name= "user"   maxlength= "/></p>        <p>" title: <input type= "Text"  name= "title"  maxlength= "/></p>        " <p> content: <textarea name= "Content"  rows= "8"  cols= "+" ></textarea></p>         <p><input type= "Submit"  name= "Submit"    Onclick= "Sendform ();"  value= "Leave a Message"/></p>    </form><?php    if ($_ post[' Submit '])  {            echo  < Script language=\ "javascript\" >alert (' Submit success! '); History.go ( -1);</script> ";         }    ? >

Two, onclick authentication:

<script language= "JavaScript" >    function SendForm  ()  {         if (Checkpost ())  {             document.addform.submit ();         }     }    function CheckPost  ()  {         if  (addform.user.value ==  "")  {             alert ("Please fill in the user name!") ");             addform.username.focus ();             return false;         }        if  (addForm.title.value.length &NBSP;&LT;&NBSP;5) &NBSP;{&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp; alert ("title cannot be less than 5 characters!") ");             addform.title.focus ();             return false;         }        return true;    } </script>


for form validation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.