JavaScript check if the form is empty function _ form effects

Source: Internet
Author: User
var textboxs = new Array (); Input collection of text type
var radiolist = new Object (); Radio type of input collection, because radio is special, to a set of tests, so here with a Hashtable, according to radio name as key to save all the Radio collection
var radioes; Each name corresponds to the Radio collection
var inputs = Myform.getelementsbytagname ("INPUT"); Take all input below the form

<script> function fn (myform) {var ipt; var textboxs = new Array (); The input set of text type var radiolist = new Object (); Radio type of input collection, because the radio is more special, to a group of detection, so here with a Hashtable, according to radio name as key to save all radio set Var radioes; Each name corresponds to the radio set var inputs = Myform.getelementsbytagname ("INPUT"); Take all input//for (var i=0; i<inputs.length; i++) {IPT = inputs[i] under form; if (Ipt.type = = "Text") {textboxs[textboxs.length] = IPT; //Originally only detects Radiobox, later to add checkbox,//Because the processing mode, as long as the change here can be, but the name is not synchronized modified:)//else if (Ipt.type = = "Radi O "| | Ipt.type = = "checkbox") {radioes = Radiolist[ipt.name]; if (!radioes) {radioes = new Array (); } Radioes[radioes.length] = IPT; Radiolist[ipt.name] = radioes; }//non-null detection//for (Var i=0 i<textboxs.length; i++) {var txt = textboxs[i]; if (txt && txt.value = "") {Alert ("Please fill:" + txt.name); Txt.focus (); return false; }//Traverse all radiobox groups//for (Var radioboxname in radiolist) {radioes = Radiolist[radioboxname]; var chk = false; Whether there is a selected VAR radio; Detects whether the group radio is selected/for (Var j=0 j<radioes.length; J + +) {var radio = radioes[j]; if (radio.checked) {chk = true; Break } if (!CHK)//Not selected {alert ("Please select:" + radioboxname); return false; } return true; } </script> <form method= "POST" action= "onsubmit=" return fn (this); " > <input type= "Text" name= "A1"/> <input type= "text" name= "A2"/> <input type= "Radio" name= "B2"/> <input type= "Radio" name= "B2"/> <input type= "Radio" name= "B2"/> <input type= "text" name= "A3"/> <input type= "Radio" Name= "B1"/> <input type= "Radio" Name= "B1"/><input type= "checkbox" Name= "C1"/> <input type= "checkbox" Name= "C1"/> <input type= "Submit"/> </ form> <form method= "POST" action= "onsubmit=" return fn (this); " > <input type= "Text" name= "A1"/> <input type= "text" name= "A2"/> <input type= "Radio" name= "B2"/> <input type= "Radio" name= "B2"/> <input type= "Radio" name= "B2"/> <input type= "text" name= "A3"/> <input type= "Radio" Name= "B1"/> <input "type= Radio" name= "B1"/> <input type= "checkbox" Name= "C1"/> <input type= "checkbox" Name= "C1"/> <input type= "Submit"/> </form>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
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.