简体   繁体   English

检查数组长度的JavaScript

[英]Check array length javascript

This is the first time I've tried to do data validation with Javascript and jQuery. 这是我第一次尝试使用Javascript和jQuery进行数据验证。

My array with errors is getting filled fine, but my problem is checking if this array is empty. 我有错误的数组可以正常填充,但是我的问题是检查此数组是否为空。 In my situation it's always true. 以我的情况,这始终是事实。 It will always alert me "errors leeg", even if the array is empty, checked with console.log(error) : 即使该数组为空,它也将始终警告我“错误leeg”,请使用console.log(error)进行检查:

<script type="text/javascript">
$(document).ready(function () 
{
    var error = [];
    $('#contactformulier').submit(function() {

      if($('input#namet').val().length == 0) {
          error['naam'] = 'Geen geldige naam ingevuld';
      } else{
          delete error['naam'];
      }

      if($('input#mailt').val().length == 0){
          error['mail'] = 'Geen geldig e-mailadres ingevuld';
      } else{
          delete error['mail'];
      }

      if($('textarea#message').val().length == 0){
          error['bericht'] = 'Geen geldig bericht ingevuld';
      } else{
          delete error['bericht'];
      }

      console.log(error);
      if (error.length < 1) {
        alert('errors leeg');
      }

      return false;
    });
});
</script>

Any suggestions? 有什么建议么?

You can only use numeric indices for arrays - at least if you want to use array functionality such as Array#length. 您只能对数组使用数字索引-至少在要使用数组功能(例如Array#length)的情况下。

ie

var error = [];
error.push({ naam: "geen geldige naam" });
error.push({ mail: "geen geldige email" });
error.length == 2

That's because you're adding properties to your array instead of indexed items. 那是因为您要向数组中添加属性,而不是为索引项添加属性。

You can add items to an array using push() : 您可以使用push()将项目添加到数组中:

error.push('Geen geldig bericht ingevuld');

And before you start validating, you clear the array: 在开始验证之前,请清除数组:

error.length = 0;
// start validation logic

The problem is that you mix up JavaScript types. 问题是您混合使用了JavaScript类型。 What you think is an associative array, in JavaScript is an object with properties and methods. 您认为这是一个关联数组,在JavaScript中是具有属性和方法的对象

So you need to define it with: 因此,您需要使用以下内容进行定义:

var error = {};

If a simple array emptiness can be checked with arr.length > 0 , object "emptiness" (ie absence of properties/methods) can be checked with jQuery method $.isEmptyObject() : 如果可以使用arr.length > 0来检查简单数组的空度,则可以使用jQuery方法$.isEmptyObject()检查对象“空度”(即缺少属性/方法$.isEmptyObject()

if ( $.isEmptyObject(error) ) { ... }

REF: https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Working_with_Objects 参考: https : //developer.mozilla.org/zh-CN/docs/JavaScript/Guide/Working_with_Objects

As others have said, you can't take the length of an Object being used as an associative array, the .length property is for real arrays. 正如其他人所说,您不能将Object的长度用作关联数组, .length属性用于实数数组。

What you can do though, is ask how many keys there are: 但是,您可以做的是询问有多少个键:

var n = Object.keys(error).length;
<script type="text/javascript">
$(document).ready(function () 
{
    var error;
    var inError;
    $('#contactformulier').submit(function() {

      error = {};
      inError = false;

      if($('input#namet').val().length == 0) {
          error['naam'] = 'Geen geldige naam ingevuld';
          inError = true;
      }

      if($('input#mailt').val().length == 0){
          error['mail'] = 'Geen geldig e-mailadres ingevuld';
          inError = true;
      }

      if($('textarea#message').val().length == 0){
          error['bericht'] = 'Geen geldig bericht ingevuld';
          inError = true;
      }

      console.log(error);
      if (inError) {
        alert('errors leeg');
      }

      return false;
    });
});
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM