Home  >  Article  >  Web Front-end  >  JavaScript function to check whether the form is empty_form effects

JavaScript function to check whether the form is empty_form effects

WBOY
WBOYOriginal
2016-05-16 19:08:011066browse

var textboxs = new Array(); // Text type input collection
var radioList = new Object(); // Radio type input collection, because Radio is special and needs to be detected group by group, so here we use one Hashtable, saves all Radio collections based on the name of the radio as Key
var radioes; // Radio collection corresponding to each name
var inputs = myform.getElementsByTagName("INPUT"); // Get the form under All inputs


[Ctrl A select all Note: If you need to introduce external Js, you need to refresh to execute
]<script> function fn(myform) { var ipt; var textboxs = new Array(); // text类型的input集合 var radioList = new Object(); // radio类型的input集合,因为Radio比较特殊,要一组一组检测,所以这里用一个Hashtable,根据radio的name作为Key来保存所有的Radio集合 var radioes; // 每一个name对应的Radio集合 var inputs = myform.getElementsByTagName("INPUT"); // 取form下的所有input // 遍历所有INPUT // for (var i=0; i<inputs.length; i++) { ipt = inputs[i ]; if (ipt.type == "text") { textboxs[textboxs.length] = ipt; } // 本来只要检测radiobox的,后来要增加checkbox, //因为处理方式一样,只要这里修改一下就可以了,但是命名没有同步修改:) // else if (ipt.type == "radio" || ipt.type == "checkbox") { radioes = radioList[ipt.name]; if (!radioes) { radioes = new Array(); } radioes[radioes.length] = ipt; radioList[ipt.name] = radioes; } } // 非空检测 // 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; } } // 遍历所有Radiobox组 // for (var radioboxName in radioList) { radioes = radioList[radioboxName]; var chk = false; // 是否有选中的 var radio; // 检测该组Radio是否都选中了 // for (var j=0; j<radioes.length; j++) { var radio = radioes[j]; if (radio.checked) { chk = true; break; } } if (!chk) // 没有选中的 { alert("please select: " + radioboxName); return false; } } return true; } </script>
Statement:
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