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

jQuery implements batch judgment of non-empty text boxes in forms (2 methods)_jquery

WBOY
Release: 2016-05-16 15:26:44
Original
1475 people have browsed it

The example in this article describes jQuery's method of batch judging whether the text boxes in the form are empty. Share it with everyone for your reference, the details are as follows:

Method 1:

<script type="text/javascript">
/*
* 批量验证表单非空
* 需要非空验证控件的样式class="mustadd"
*/
$(".mustadd").each(function(){
if($(this).val() == ''){
  alert("该项不可为空!");
  $(this).focus();
  return false;
}
})
</script>

Copy after login

Method 2:

<script type="text/javascript">
//批量验证表单非空
function checkForm(arr){
  for(var i=0;i<arr.length;i++){
  if($("#"+arr[i][0]).val()==''){
    alert(arr[i][1]);
    $("#"+arr[i][0]).focus();
    return false;
  }
  }
  return true;
};
//调用方式
var arr=new Array(
  new Array('userName','用户名不可为空!'),
  new Array('passwd','密码不可为空!'),
));
if(!checkForm(arr)){
  return false;
}
</script>

Copy after login

I hope this article will be helpful to everyone in jQuery programming.

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