Xác thực tệp JavaScript trực tuyến

Ví dụ:

var constraints = {
  confirmPassword: {
    equality: "password"
  }
};

validate[{password: "foo", confirmPassword: "foo"}, constraints];
// => undefined

validate[{password: "foo", confirmPassword: "bar"}, constraints];
// => {confirmPassword: ["Confirm password is not equal to password"]}

constraints = {
  complexAttribute: {
    equality: {
      attribute: "otherComplexAttribute",
      message: "is not complex enough",
      comparator: function[v1, v2] {
        return JSON.stringify[v1] === JSON.stringify[v2];
      }
    }
  }
};

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
// => undefined

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
// => {complexAttribute: ["Complex attribute is not complex enough"]}
0 là một email hoàn toàn hợp lệ nhưng rất có thể John đã quên viết
var constraints = {
  confirmPassword: {
    equality: "password"
  }
};

validate[{password: "foo", confirmPassword: "foo"}, constraints];
// => undefined

validate[{password: "foo", confirmPassword: "bar"}, constraints];
// => {confirmPassword: ["Confirm password is not equal to password"]}

constraints = {
  complexAttribute: {
    equality: {
      attribute: "otherComplexAttribute",
      message: "is not complex enough",
      comparator: function[v1, v2] {
        return JSON.stringify[v1] === JSON.stringify[v2];
      }
    }
  }
};

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
// => undefined

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
// => {complexAttribute: ["Complex attribute is not complex enough"]}
1 ở cuối

Xác thực. js cố gắng thực dụng và cho phép hầu hết các email hợp lệ nhưng cố gắng phát hiện các lỗi chính tả phổ biến như quên TLD.
Nếu bạn muốn biết thêm về xác thực email, hãy xem bài viết trên Wikipedia và trang email về biểu thức chính quy. thông tin là nơi tốt để bắt đầu.

Bạn có thể tùy chỉnh biểu thức chính quy được sử dụng bằng cách đặt

var constraints = {
  confirmPassword: {
    equality: "password"
  }
};

validate[{password: "foo", confirmPassword: "foo"}, constraints];
// => undefined

validate[{password: "foo", confirmPassword: "bar"}, constraints];
// => {confirmPassword: ["Confirm password is not equal to password"]}

constraints = {
  complexAttribute: {
    equality: {
      attribute: "otherComplexAttribute",
      message: "is not complex enough",
      comparator: function[v1, v2] {
        return JSON.stringify[v1] === JSON.stringify[v2];
      }
    }
  }
};

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
// => undefined

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
// => {complexAttribute: ["Complex attribute is not complex enough"]}
2 thành biểu thức chính quy bạn chọn, chỉ cần nhớ rằng biểu thức chính quy javascript thực hiện khớp chuỗi con

Tin nhắn mặc định không phải là một email hợp lệ và như thường lệ, bạn có thể ghi đè nó bằng tùy chọn tin nhắn hoặc bằng cách đặt

var constraints = {
  confirmPassword: {
    equality: "password"
  }
};

validate[{password: "foo", confirmPassword: "foo"}, constraints];
// => undefined

validate[{password: "foo", confirmPassword: "bar"}, constraints];
// => {confirmPassword: ["Confirm password is not equal to password"]}

constraints = {
  complexAttribute: {
    equality: {
      attribute: "otherComplexAttribute",
      message: "is not complex enough",
      comparator: function[v1, v2] {
        return JSON.stringify[v1] === JSON.stringify[v2];
      }
    }
  }
};

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
// => undefined

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
// => {complexAttribute: ["Complex attribute is not complex enough"]}
3

var constraints = {
  from: {
    email: true
  }
};

validate[{from: null}, constraints];
// => undefined

validate[{from: ""}, constraints];
// => {"email": ["From is not a valid email"]}

validate[{from: "[email protected]"}, constraints];
// => {"email": ["From is not a valid email"]}

// Any TLD is allowed
validate[{from: "[email protected]"}, constraints];
// => undefined

// Upper cased emails are allowed
validate[{from: "[email protected]"}, constraints];
// => undefined

constraints = {
  from: {
    email: {
      message: "doesn't look like a valid email"
    }
  }
};

validate[{from: "foobar"}, constraints];
// => {"email": ["From doesn't look like a valid email"]}

// It allows unicode
validate[{from: "first.lä[email protected]"}, constraints];
// => undefined

Có thể sử dụng trình xác nhận đẳng thức để xác minh rằng một thuộc tính luôn bằng một thuộc tính khác.
Điều này hữu ích khi nhập "xác nhận mật khẩu" chẳng hạn.

Bạn chỉ định thuộc tính nào bằng cách sử dụng tên của nó làm tùy chọn cho trình xác thực hoặc bằng cách cung cấp thuộc tính tùy chọn

Theo mặc định,

var constraints = {
  confirmPassword: {
    equality: "password"
  }
};

validate[{password: "foo", confirmPassword: "foo"}, constraints];
// => undefined

validate[{password: "foo", confirmPassword: "bar"}, constraints];
// => {confirmPassword: ["Confirm password is not equal to password"]}

constraints = {
  complexAttribute: {
    equality: {
      attribute: "otherComplexAttribute",
      message: "is not complex enough",
      comparator: function[v1, v2] {
        return JSON.stringify[v1] === JSON.stringify[v2];
      }
    }
  }
};

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
// => undefined

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
// => {complexAttribute: ["Complex attribute is not complex enough"]}
4 được sử dụng để kiểm tra chất lượng, bạn cần xác thực các đối tượng phức tạp hơn, bạn có thể cung cấp một hàm bằng cách sử dụng tùy chọn bộ so sánh, đây phải là một hàm chấp nhận hai đối số và trả về
var constraints = {
  confirmPassword: {
    equality: "password"
  }
};

validate[{password: "foo", confirmPassword: "foo"}, constraints];
// => undefined

validate[{password: "foo", confirmPassword: "bar"}, constraints];
// => {confirmPassword: ["Confirm password is not equal to password"]}

constraints = {
  complexAttribute: {
    equality: {
      attribute: "otherComplexAttribute",
      message: "is not complex enough",
      comparator: function[v1, v2] {
        return JSON.stringify[v1] === JSON.stringify[v2];
      }
    }
  }
};

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
// => undefined

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
// => {complexAttribute: ["Complex attribute is not complex enough"]}
5 nếu các đối tượng bằng nhau và
var constraints = {
  confirmPassword: {
    equality: "password"
  }
};

validate[{password: "foo", confirmPassword: "foo"}, constraints];
// => undefined

validate[{password: "foo", confirmPassword: "bar"}, constraints];
// => {confirmPassword: ["Confirm password is not equal to password"]}

constraints = {
  complexAttribute: {
    equality: {
      attribute: "otherComplexAttribute",
      message: "is not complex enough",
      comparator: function[v1, v2] {
        return JSON.stringify[v1] === JSON.stringify[v2];
      }
    }
  }
};

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
// => undefined

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
// => {complexAttribute: ["Complex attribute is not complex enough"]}
6 nếu chúng không bằng nhau

Thông báo mặc định không bằng %{attribute}

var constraints = {
  confirmPassword: {
    equality: "password"
  }
};

validate[{password: "foo", confirmPassword: "foo"}, constraints];
// => undefined

validate[{password: "foo", confirmPassword: "bar"}, constraints];
// => {confirmPassword: ["Confirm password is not equal to password"]}

constraints = {
  complexAttribute: {
    equality: {
      attribute: "otherComplexAttribute",
      message: "is not complex enough",
      comparator: function[v1, v2] {
        return JSON.stringify[v1] === JSON.stringify[v2];
      }
    }
  }
};

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
// => undefined

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
// => {complexAttribute: ["Complex attribute is not complex enough"]}
7

var constraints = {
  confirmPassword: {
    equality: "password"
  }
};

validate[{password: "foo", confirmPassword: "foo"}, constraints];
// => undefined

validate[{password: "foo", confirmPassword: "bar"}, constraints];
// => {confirmPassword: ["Confirm password is not equal to password"]}

constraints = {
  complexAttribute: {
    equality: {
      attribute: "otherComplexAttribute",
      message: "is not complex enough",
      comparator: function[v1, v2] {
        return JSON.stringify[v1] === JSON.stringify[v2];
      }
    }
  }
};

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
// => undefined

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
// => {complexAttribute: ["Complex attribute is not complex enough"]}

Trình xác thực loại trừ hữu ích để hạn chế các giá trị nhất định.
Nó kiểm tra xem giá trị đã cho không có trong danh sách được cung cấp bởi tùy chọn bên trong.

Bạn có thể chỉ định bên trong dưới dạng danh sách hoặc dưới dạng đối tượng [trong trường hợp đó, các khóa của đối tượng được sử dụng].
Thông báo mặc định là ^%{value} bị hạn chế và có thể thay đổi bằng cách đặt

var constraints = {
  confirmPassword: {
    equality: "password"
  }
};

validate[{password: "foo", confirmPassword: "foo"}, constraints];
// => undefined

validate[{password: "foo", confirmPassword: "bar"}, constraints];
// => {confirmPassword: ["Confirm password is not equal to password"]}

constraints = {
  complexAttribute: {
    equality: {
      attribute: "otherComplexAttribute",
      message: "is not complex enough",
      comparator: function[v1, v2] {
        return JSON.stringify[v1] === JSON.stringify[v2];
      }
    }
  }
};

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
// => undefined

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
// => {complexAttribute: ["Complex attribute is not complex enough"]}
8

var constraints = {
  confirmPassword: {
    equality: "password"
  }
};

validate[{password: "foo", confirmPassword: "foo"}, constraints];
// => undefined

validate[{password: "foo", confirmPassword: "bar"}, constraints];
// => {confirmPassword: ["Confirm password is not equal to password"]}

constraints = {
  complexAttribute: {
    equality: {
      attribute: "otherComplexAttribute",
      message: "is not complex enough",
      comparator: function[v1, v2] {
        return JSON.stringify[v1] === JSON.stringify[v2];
      }
    }
  }
};

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
// => undefined

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
// => {complexAttribute: ["Complex attribute is not complex enough"]}
1

Trình xác thực định dạng sẽ xác thực một giá trị dựa trên biểu thức chính quy mà bạn chọn. Thông báo mặc định nếu giá trị không khớp là không hợp lệ, vì vậy bạn có thể muốn tùy chỉnh thông báo đó bằng cách cài đặt thông báo thành nội dung nào đó trong tùy chọn hoặc bằng cách đặt thông báo mặc định chung mới bằng cách sử dụng

var constraints = {
  confirmPassword: {
    equality: "password"
  }
};

validate[{password: "foo", confirmPassword: "foo"}, constraints];
// => undefined

validate[{password: "foo", confirmPassword: "bar"}, constraints];
// => {confirmPassword: ["Confirm password is not equal to password"]}

constraints = {
  complexAttribute: {
    equality: {
      attribute: "otherComplexAttribute",
      message: "is not complex enough",
      comparator: function[v1, v2] {
        return JSON.stringify[v1] === JSON.stringify[v2];
      }
    }
  }
};

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
// => undefined

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
// => {complexAttribute: ["Complex attribute is not complex enough"]}
9

Tùy chọn mẫu có thể là biểu thức chính quy javascript hoặc chuỗi sẽ được chuyển đến hàm tạo RegExp. Nếu mẫu là một chuỗi và bạn muốn chỉ định cờ, bạn có thể sử dụng tùy chọn cờ

Xin lưu ý rằng toàn bộ chuỗi phải khớp với biểu thức chính quy, không chỉ một phần của giá trị

var constraints = {
  confirmPassword: {
    equality: "password"
  }
};

validate[{password: "foo", confirmPassword: "foo"}, constraints];
// => undefined

validate[{password: "foo", confirmPassword: "bar"}, constraints];
// => {confirmPassword: ["Confirm password is not equal to password"]}

constraints = {
  complexAttribute: {
    equality: {
      attribute: "otherComplexAttribute",
      message: "is not complex enough",
      comparator: function[v1, v2] {
        return JSON.stringify[v1] === JSON.stringify[v2];
      }
    }
  }
};

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
// => undefined

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
// => {complexAttribute: ["Complex attribute is not complex enough"]}
3

Trình xác thực bao gồm hữu ích để xác thực đầu vào từ danh sách thả xuống chẳng hạn.
Nó kiểm tra xem giá trị đã cho có tồn tại trong danh sách được cung cấp bởi tùy chọn bên trong không.

Bạn có thể chỉ định bên trong dưới dạng danh sách hoặc dưới dạng đối tượng [trong trường hợp đó, các khóa của đối tượng được sử dụng].
Thông báo mặc định là ^%{value} không có trong danh sách và có thể thay đổi bằng cách đặt

var constraints = {
  confirmPassword: {
    equality: "password"
  }
};

validate[{password: "foo", confirmPassword: "foo"}, constraints];
// => undefined

validate[{password: "foo", confirmPassword: "bar"}, constraints];
// => {confirmPassword: ["Confirm password is not equal to password"]}

constraints = {
  complexAttribute: {
    equality: {
      attribute: "otherComplexAttribute",
      message: "is not complex enough",
      comparator: function[v1, v2] {
        return JSON.stringify[v1] === JSON.stringify[v2];
      }
    }
  }
};

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
// => undefined

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
// => {complexAttribute: ["Complex attribute is not complex enough"]}
10

var constraints = {
  confirmPassword: {
    equality: "password"
  }
};

validate[{password: "foo", confirmPassword: "foo"}, constraints];
// => undefined

validate[{password: "foo", confirmPassword: "bar"}, constraints];
// => {confirmPassword: ["Confirm password is not equal to password"]}

constraints = {
  complexAttribute: {
    equality: {
      attribute: "otherComplexAttribute",
      message: "is not complex enough",
      comparator: function[v1, v2] {
        return JSON.stringify[v1] === JSON.stringify[v2];
      }
    }
  }
};

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
// => undefined

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
// => {complexAttribute: ["Complex attribute is not complex enough"]}
5

Trình xác thực độ dài sẽ kiểm tra độ dài của chuỗi.
Bất kỳ đối tượng nào có thuộc tính

var constraints = {
  confirmPassword: {
    equality: "password"
  }
};

validate[{password: "foo", confirmPassword: "foo"}, constraints];
// => undefined

validate[{password: "foo", confirmPassword: "bar"}, constraints];
// => {confirmPassword: ["Confirm password is not equal to password"]}

constraints = {
  complexAttribute: {
    equality: {
      attribute: "otherComplexAttribute",
      message: "is not complex enough",
      comparator: function[v1, v2] {
        return JSON.stringify[v1] === JSON.stringify[v2];
      }
    }
  }
};

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
// => undefined

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
// => {complexAttribute: ["Complex attribute is not complex enough"]}
11 đều có thể được xác thực nhưng tất cả các thông báo lỗi mặc định đề cập đến các chuỗi, vì vậy hãy đảm bảo bạn ghi đè chúng nếu bạn định xác thực các mảng bằng cách sử dụng mã này.

Bạn có thể chỉ định các ràng buộc về độ dài sau

là Giá trị phải có chính xác độ dài này. Lỗi mặc định là sai độ dài [phải là %{count} ký tự]tối thiểu Giá trị không được ngắn hơn giá trị này. Lỗi mặc định quá ngắn [tối thiểu là %{count} ký tự]tối đa Giá trị không được dài hơn giá trị này. Lỗi mặc định quá dài [tối đa là %{count} ký tự]

Bạn có thể chỉ định thông báo lỗi bằng các tùy chọn notValid, falseLength, tooShort và tooLong. Các giá trị mặc định có độ dài không chính xác, độ dài sai [phải là %{count} ký tự], quá ngắn [tối thiểu là %{count} ký tự] và quá dài [tối đa là %{count} ký tự] tương ứng

Như bạn có thể nhận thấy, bạn có thể sử dụng

var constraints = {
  confirmPassword: {
    equality: "password"
  }
};

validate[{password: "foo", confirmPassword: "foo"}, constraints];
// => undefined

validate[{password: "foo", confirmPassword: "bar"}, constraints];
// => {confirmPassword: ["Confirm password is not equal to password"]}

constraints = {
  complexAttribute: {
    equality: {
      attribute: "otherComplexAttribute",
      message: "is not complex enough",
      comparator: function[v1, v2] {
        return JSON.stringify[v1] === JSON.stringify[v2];
      }
    }
  }
};

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
// => undefined

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
// => {complexAttribute: ["Complex attribute is not complex enough"]}
12 làm trình giữ chỗ cho ràng buộc thực tế và nó sẽ được thay thế cho bạn

Các thông báo mặc định cũng có thể được thay đổi bằng cách đặt các thuộc tính sau trên

var constraints = {
  confirmPassword: {
    equality: "password"
  }
};

validate[{password: "foo", confirmPassword: "foo"}, constraints];
// => undefined

validate[{password: "foo", confirmPassword: "bar"}, constraints];
// => {confirmPassword: ["Confirm password is not equal to password"]}

constraints = {
  complexAttribute: {
    equality: {
      attribute: "otherComplexAttribute",
      message: "is not complex enough",
      comparator: function[v1, v2] {
        return JSON.stringify[v1] === JSON.stringify[v2];
      }
    }
  }
};

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
// => undefined

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
// => {complexAttribute: ["Complex attribute is not complex enough"]}
13

  • var constraints = {
      confirmPassword: {
        equality: "password"
      }
    };
    
    validate[{password: "foo", confirmPassword: "foo"}, constraints];
    // => undefined
    
    validate[{password: "foo", confirmPassword: "bar"}, constraints];
    // => {confirmPassword: ["Confirm password is not equal to password"]}
    
    constraints = {
      complexAttribute: {
        equality: {
          attribute: "otherComplexAttribute",
          message: "is not complex enough",
          comparator: function[v1, v2] {
            return JSON.stringify[v1] === JSON.stringify[v2];
          }
        }
      }
    };
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
    // => undefined
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
    // => {complexAttribute: ["Complex attribute is not complex enough"]}
    14
  • var constraints = {
      confirmPassword: {
        equality: "password"
      }
    };
    
    validate[{password: "foo", confirmPassword: "foo"}, constraints];
    // => undefined
    
    validate[{password: "foo", confirmPassword: "bar"}, constraints];
    // => {confirmPassword: ["Confirm password is not equal to password"]}
    
    constraints = {
      complexAttribute: {
        equality: {
          attribute: "otherComplexAttribute",
          message: "is not complex enough",
          comparator: function[v1, v2] {
            return JSON.stringify[v1] === JSON.stringify[v2];
          }
        }
      }
    };
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
    // => undefined
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
    // => {complexAttribute: ["Complex attribute is not complex enough"]}
    15
  • var constraints = {
      confirmPassword: {
        equality: "password"
      }
    };
    
    validate[{password: "foo", confirmPassword: "foo"}, constraints];
    // => undefined
    
    validate[{password: "foo", confirmPassword: "bar"}, constraints];
    // => {confirmPassword: ["Confirm password is not equal to password"]}
    
    constraints = {
      complexAttribute: {
        equality: {
          attribute: "otherComplexAttribute",
          message: "is not complex enough",
          comparator: function[v1, v2] {
            return JSON.stringify[v1] === JSON.stringify[v2];
          }
        }
      }
    };
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
    // => undefined
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
    // => {complexAttribute: ["Complex attribute is not complex enough"]}
    16
  • var constraints = {
      confirmPassword: {
        equality: "password"
      }
    };
    
    validate[{password: "foo", confirmPassword: "foo"}, constraints];
    // => undefined
    
    validate[{password: "foo", confirmPassword: "bar"}, constraints];
    // => {confirmPassword: ["Confirm password is not equal to password"]}
    
    constraints = {
      complexAttribute: {
        equality: {
          attribute: "otherComplexAttribute",
          message: "is not complex enough",
          comparator: function[v1, v2] {
            return JSON.stringify[v1] === JSON.stringify[v2];
          }
        }
      }
    };
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
    // => undefined
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
    // => {complexAttribute: ["Complex attribute is not complex enough"]}
    17

Bạn cũng có thể sử dụng thông báo làm thông báo cho tất cả các lỗi [điều này sẽ ghi đè bất kỳ lỗi tùy chỉnh nào khác]

Theo mặc định, số lượng ký tự được tính [sử dụng thuộc tính

var constraints = {
  confirmPassword: {
    equality: "password"
  }
};

validate[{password: "foo", confirmPassword: "foo"}, constraints];
// => undefined

validate[{password: "foo", confirmPassword: "bar"}, constraints];
// => {confirmPassword: ["Confirm password is not equal to password"]}

constraints = {
  complexAttribute: {
    equality: {
      attribute: "otherComplexAttribute",
      message: "is not complex enough",
      comparator: function[v1, v2] {
        return JSON.stringify[v1] === JSON.stringify[v2];
      }
    }
  }
};

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
// => undefined

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
// => {complexAttribute: ["Complex attribute is not complex enough"]}
11], nếu bạn muốn đếm thứ gì khác, bạn có thể chỉ định tùy chọn mã thông báo. Đây phải là một hàm nhận một đối số [giá trị] và trả về một giá trị phải là

Trình mã thông báo sẽ không bao giờ được gọi với đối số là

var constraints = {
  confirmPassword: {
    equality: "password"
  }
};

validate[{password: "foo", confirmPassword: "foo"}, constraints];
// => undefined

validate[{password: "foo", confirmPassword: "bar"}, constraints];
// => {confirmPassword: ["Confirm password is not equal to password"]}

constraints = {
  complexAttribute: {
    equality: {
      attribute: "otherComplexAttribute",
      message: "is not complex enough",
      comparator: function[v1, v2] {
        return JSON.stringify[v1] === JSON.stringify[v2];
      }
    }
  }
};

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
// => undefined

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
// => {complexAttribute: ["Complex attribute is not complex enough"]}
19 hoặc
var constraints = {
  confirmPassword: {
    equality: "password"
  }
};

validate[{password: "foo", confirmPassword: "foo"}, constraints];
// => undefined

validate[{password: "foo", confirmPassword: "bar"}, constraints];
// => {confirmPassword: ["Confirm password is not equal to password"]}

constraints = {
  complexAttribute: {
    equality: {
      attribute: "otherComplexAttribute",
      message: "is not complex enough",
      comparator: function[v1, v2] {
        return JSON.stringify[v1] === JSON.stringify[v2];
      }
    }
  }
};

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
// => undefined

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
// => {complexAttribute: ["Complex attribute is not complex enough"]}
30

Một khi điều quan trọng cần lưu ý là giá trị cần phải có giá trị số cho thuộc tính

var constraints = {
  confirmPassword: {
    equality: "password"
  }
};

validate[{password: "foo", confirmPassword: "foo"}, constraints];
// => undefined

validate[{password: "foo", confirmPassword: "bar"}, constraints];
// => {confirmPassword: ["Confirm password is not equal to password"]}

constraints = {
  complexAttribute: {
    equality: {
      attribute: "otherComplexAttribute",
      message: "is not complex enough",
      comparator: function[v1, v2] {
        return JSON.stringify[v1] === JSON.stringify[v2];
      }
    }
  }
};

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
// => undefined

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
// => {complexAttribute: ["Complex attribute is not complex enough"]}
11 hoặc thông báo có độ dài không chính xác được trả về.
Một lỗi cũng được ghi vào bảng điều khiển vì đây được coi là lỗi mã hóa.

var constraints = {
  confirmPassword: {
    equality: "password"
  }
};

validate[{password: "foo", confirmPassword: "foo"}, constraints];
// => undefined

validate[{password: "foo", confirmPassword: "bar"}, constraints];
// => {confirmPassword: ["Confirm password is not equal to password"]}

constraints = {
  complexAttribute: {
    equality: {
      attribute: "otherComplexAttribute",
      message: "is not complex enough",
      comparator: function[v1, v2] {
        return JSON.stringify[v1] === JSON.stringify[v2];
      }
    }
  }
};

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
// => undefined

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
// => {complexAttribute: ["Complex attribute is not complex enough"]}
7

Trình xác thực số sẽ chỉ cho phép các số. Mỗi chuỗi mặc định được ép thành số bằng cách sử dụng toán tử

var constraints = {
  confirmPassword: {
    equality: "password"
  }
};

validate[{password: "foo", confirmPassword: "foo"}, constraints];
// => undefined

validate[{password: "foo", confirmPassword: "bar"}, constraints];
// => {confirmPassword: ["Confirm password is not equal to password"]}

constraints = {
  complexAttribute: {
    equality: {
      attribute: "otherComplexAttribute",
      message: "is not complex enough",
      comparator: function[v1, v2] {
        return JSON.stringify[v1] === JSON.stringify[v2];
      }
    }
  }
};

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
// => undefined

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
// => {complexAttribute: ["Complex attribute is not complex enough"]}
32. Nếu điều này là không mong muốn, bạn có thể đặt tùy chọn noStrings thành
var constraints = {
  confirmPassword: {
    equality: "password"
  }
};

validate[{password: "foo", confirmPassword: "foo"}, constraints];
// => undefined

validate[{password: "foo", confirmPassword: "bar"}, constraints];
// => {confirmPassword: ["Confirm password is not equal to password"]}

constraints = {
  complexAttribute: {
    equality: {
      attribute: "otherComplexAttribute",
      message: "is not complex enough",
      comparator: function[v1, v2] {
        return JSON.stringify[v1] === JSON.stringify[v2];
      }
    }
  }
};

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
// => undefined

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
// => {complexAttribute: ["Complex attribute is not complex enough"]}
5 để tắt hành vi này

Có thể áp dụng các ràng buộc sau

onlyIntegerSố thực sẽ không được phép. Thông báo lỗi phải là một số nguyênstrictCho phép xác thực chuỗi nghiêm ngặt hơn. Các số 0 đứng đầu sẽ không được phép và số không được định dạng sai. lớn hơn Đầu vào phải lớn hơn giá trị này. Thông báo lỗi phải lớn hơn %{count}greaterThanOrEqualToĐầu vào ít nhất phải có giá trị này. Thông báo lỗi phải lớn hơn hoặc bằng %{count}equalToĐầu vào phải chính xác là giá trị này. Thông báo lỗi phải bằng %{count}lessThanOrEqualToGiá trị đầu vào tối đa có thể là giá trị này. Thông báo lỗi phải nhỏ hơn hoặc bằng %{count}lessThanĐầu vào phải nhỏ hơn giá trị này. Thông báo lỗi phải nhỏ hơn %{count}divisiableByĐầu vào phải chia hết cho giá trị này. Thông báo lỗi phải chia hết cho %{count}oddĐầu vào phải là số lẻ. Thông báo lỗi phải là số lẻĐầu vào phải là số chẵn. Thông báo lỗi là phải chẵn

Nếu bạn muốn một thông báo lỗi tùy chỉnh, bạn có thể chỉ định nó bằng cách sử dụng tùy chọn thông báo hoặc bằng cách cài đặt chỉ định các thông báo sau

  • var constraints = {
      confirmPassword: {
        equality: "password"
      }
    };
    
    validate[{password: "foo", confirmPassword: "foo"}, constraints];
    // => undefined
    
    validate[{password: "foo", confirmPassword: "bar"}, constraints];
    // => {confirmPassword: ["Confirm password is not equal to password"]}
    
    constraints = {
      complexAttribute: {
        equality: {
          attribute: "otherComplexAttribute",
          message: "is not complex enough",
          comparator: function[v1, v2] {
            return JSON.stringify[v1] === JSON.stringify[v2];
          }
        }
      }
    };
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
    // => undefined
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
    // => {complexAttribute: ["Complex attribute is not complex enough"]}
    14
  • var constraints = {
      confirmPassword: {
        equality: "password"
      }
    };
    
    validate[{password: "foo", confirmPassword: "foo"}, constraints];
    // => undefined
    
    validate[{password: "foo", confirmPassword: "bar"}, constraints];
    // => {confirmPassword: ["Confirm password is not equal to password"]}
    
    constraints = {
      complexAttribute: {
        equality: {
          attribute: "otherComplexAttribute",
          message: "is not complex enough",
          comparator: function[v1, v2] {
            return JSON.stringify[v1] === JSON.stringify[v2];
          }
        }
      }
    };
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
    // => undefined
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
    // => {complexAttribute: ["Complex attribute is not complex enough"]}
    35
  • var constraints = {
      confirmPassword: {
        equality: "password"
      }
    };
    
    validate[{password: "foo", confirmPassword: "foo"}, constraints];
    // => undefined
    
    validate[{password: "foo", confirmPassword: "bar"}, constraints];
    // => {confirmPassword: ["Confirm password is not equal to password"]}
    
    constraints = {
      complexAttribute: {
        equality: {
          attribute: "otherComplexAttribute",
          message: "is not complex enough",
          comparator: function[v1, v2] {
            return JSON.stringify[v1] === JSON.stringify[v2];
          }
        }
      }
    };
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
    // => undefined
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
    // => {complexAttribute: ["Complex attribute is not complex enough"]}
    36
  • var constraints = {
      confirmPassword: {
        equality: "password"
      }
    };
    
    validate[{password: "foo", confirmPassword: "foo"}, constraints];
    // => undefined
    
    validate[{password: "foo", confirmPassword: "bar"}, constraints];
    // => {confirmPassword: ["Confirm password is not equal to password"]}
    
    constraints = {
      complexAttribute: {
        equality: {
          attribute: "otherComplexAttribute",
          message: "is not complex enough",
          comparator: function[v1, v2] {
            return JSON.stringify[v1] === JSON.stringify[v2];
          }
        }
      }
    };
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
    // => undefined
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
    // => {complexAttribute: ["Complex attribute is not complex enough"]}
    37
  • var constraints = {
      confirmPassword: {
        equality: "password"
      }
    };
    
    validate[{password: "foo", confirmPassword: "foo"}, constraints];
    // => undefined
    
    validate[{password: "foo", confirmPassword: "bar"}, constraints];
    // => {confirmPassword: ["Confirm password is not equal to password"]}
    
    constraints = {
      complexAttribute: {
        equality: {
          attribute: "otherComplexAttribute",
          message: "is not complex enough",
          comparator: function[v1, v2] {
            return JSON.stringify[v1] === JSON.stringify[v2];
          }
        }
      }
    };
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
    // => undefined
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
    // => {complexAttribute: ["Complex attribute is not complex enough"]}
    38
  • var constraints = {
      confirmPassword: {
        equality: "password"
      }
    };
    
    validate[{password: "foo", confirmPassword: "foo"}, constraints];
    // => undefined
    
    validate[{password: "foo", confirmPassword: "bar"}, constraints];
    // => {confirmPassword: ["Confirm password is not equal to password"]}
    
    constraints = {
      complexAttribute: {
        equality: {
          attribute: "otherComplexAttribute",
          message: "is not complex enough",
          comparator: function[v1, v2] {
            return JSON.stringify[v1] === JSON.stringify[v2];
          }
        }
      }
    };
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
    // => undefined
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
    // => {complexAttribute: ["Complex attribute is not complex enough"]}
    39
  • var constraints = {
      confirmPassword: {
        equality: "password"
      }
    };
    
    validate[{password: "foo", confirmPassword: "foo"}, constraints];
    // => undefined
    
    validate[{password: "foo", confirmPassword: "bar"}, constraints];
    // => {confirmPassword: ["Confirm password is not equal to password"]}
    
    constraints = {
      complexAttribute: {
        equality: {
          attribute: "otherComplexAttribute",
          message: "is not complex enough",
          comparator: function[v1, v2] {
            return JSON.stringify[v1] === JSON.stringify[v2];
          }
        }
      }
    };
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
    // => undefined
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
    // => {complexAttribute: ["Complex attribute is not complex enough"]}
    50
  • var constraints = {
      confirmPassword: {
        equality: "password"
      }
    };
    
    validate[{password: "foo", confirmPassword: "foo"}, constraints];
    // => undefined
    
    validate[{password: "foo", confirmPassword: "bar"}, constraints];
    // => {confirmPassword: ["Confirm password is not equal to password"]}
    
    constraints = {
      complexAttribute: {
        equality: {
          attribute: "otherComplexAttribute",
          message: "is not complex enough",
          comparator: function[v1, v2] {
            return JSON.stringify[v1] === JSON.stringify[v2];
          }
        }
      }
    };
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
    // => undefined
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
    // => {complexAttribute: ["Complex attribute is not complex enough"]}
    51
  • var constraints = {
      confirmPassword: {
        equality: "password"
      }
    };
    
    validate[{password: "foo", confirmPassword: "foo"}, constraints];
    // => undefined
    
    validate[{password: "foo", confirmPassword: "bar"}, constraints];
    // => {confirmPassword: ["Confirm password is not equal to password"]}
    
    constraints = {
      complexAttribute: {
        equality: {
          attribute: "otherComplexAttribute",
          message: "is not complex enough",
          comparator: function[v1, v2] {
            return JSON.stringify[v1] === JSON.stringify[v2];
          }
        }
      }
    };
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
    // => undefined
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
    // => {complexAttribute: ["Complex attribute is not complex enough"]}
    52
  • var constraints = {
      confirmPassword: {
        equality: "password"
      }
    };
    
    validate[{password: "foo", confirmPassword: "foo"}, constraints];
    // => undefined
    
    validate[{password: "foo", confirmPassword: "bar"}, constraints];
    // => {confirmPassword: ["Confirm password is not equal to password"]}
    
    constraints = {
      complexAttribute: {
        equality: {
          attribute: "otherComplexAttribute",
          message: "is not complex enough",
          comparator: function[v1, v2] {
            return JSON.stringify[v1] === JSON.stringify[v2];
          }
        }
      }
    };
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
    // => undefined
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
    // => {complexAttribute: ["Complex attribute is not complex enough"]}
    53
var constraints = {
  from: {
    email: true
  }
};

validate[{from: null}, constraints];
// => undefined

validate[{from: ""}, constraints];
// => {"email": ["From is not a valid email"]}

validate[{from: "[email protected]"}, constraints];
// => {"email": ["From is not a valid email"]}

// Any TLD is allowed
validate[{from: "[email protected]"}, constraints];
// => undefined

// Upper cased emails are allowed
validate[{from: "[email protected]"}, constraints];
// => undefined

constraints = {
  from: {
    email: {
      message: "doesn't look like a valid email"
    }
  }
};

validate[{from: "foobar"}, constraints];
// => {"email": ["From doesn't look like a valid email"]}

// It allows unicode
validate[{from: "first.lä[email protected]"}, constraints];
// => undefined
0

Trình xác thực hiện diện xác thực rằng giá trị được xác định. Trình xác thực này có thể sẽ là trình xác thực được sử dụng nhiều nhất, nó tương ứng với thuộc tính bắt buộc của HTML5.
Bạn có thể sử dụng tùy chọn tin nhắn để tùy chỉnh tin nhắn. Thông báo mặc định không được để trống và có thể thay đổi bằng cách đặt

var constraints = {
  confirmPassword: {
    equality: "password"
  }
};

validate[{password: "foo", confirmPassword: "foo"}, constraints];
// => undefined

validate[{password: "foo", confirmPassword: "bar"}, constraints];
// => {confirmPassword: ["Confirm password is not equal to password"]}

constraints = {
  complexAttribute: {
    equality: {
      attribute: "otherComplexAttribute",
      message: "is not complex enough",
      comparator: function[v1, v2] {
        return JSON.stringify[v1] === JSON.stringify[v2];
      }
    }
  }
};

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
// => undefined

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
// => {complexAttribute: ["Complex attribute is not complex enough"]}
54.

Đây là những giá trị được coi là rỗng

Ngoài ra, bạn có thể đặt

var constraints = {
  confirmPassword: {
    equality: "password"
  }
};

validate[{password: "foo", confirmPassword: "foo"}, constraints];
// => undefined

validate[{password: "foo", confirmPassword: "bar"}, constraints];
// => {confirmPassword: ["Confirm password is not equal to password"]}

constraints = {
  complexAttribute: {
    equality: {
      attribute: "otherComplexAttribute",
      message: "is not complex enough",
      comparator: function[v1, v2] {
        return JSON.stringify[v1] === JSON.stringify[v2];
      }
    }
  }
};

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
// => undefined

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
// => {complexAttribute: ["Complex attribute is not complex enough"]}
55 thành
var constraints = {
  confirmPassword: {
    equality: "password"
  }
};

validate[{password: "foo", confirmPassword: "foo"}, constraints];
// => undefined

validate[{password: "foo", confirmPassword: "bar"}, constraints];
// => {confirmPassword: ["Confirm password is not equal to password"]}

constraints = {
  complexAttribute: {
    equality: {
      attribute: "otherComplexAttribute",
      message: "is not complex enough",
      comparator: function[v1, v2] {
        return JSON.stringify[v1] === JSON.stringify[v2];
      }
    }
  }
};

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
// => undefined

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
// => {complexAttribute: ["Complex attribute is not complex enough"]}
6 để không cho phép các giá trị sau

  • var constraints = {
      confirmPassword: {
        equality: "password"
      }
    };
    
    validate[{password: "foo", confirmPassword: "foo"}, constraints];
    // => undefined
    
    validate[{password: "foo", confirmPassword: "bar"}, constraints];
    // => {confirmPassword: ["Confirm password is not equal to password"]}
    
    constraints = {
      complexAttribute: {
        equality: {
          attribute: "otherComplexAttribute",
          message: "is not complex enough",
          comparator: function[v1, v2] {
            return JSON.stringify[v1] === JSON.stringify[v2];
          }
        }
      }
    };
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
    // => undefined
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
    // => {complexAttribute: ["Complex attribute is not complex enough"]}
    57 [đối tượng trống]
  • var constraints = {
      confirmPassword: {
        equality: "password"
      }
    };
    
    validate[{password: "foo", confirmPassword: "foo"}, constraints];
    // => undefined
    
    validate[{password: "foo", confirmPassword: "bar"}, constraints];
    // => {confirmPassword: ["Confirm password is not equal to password"]}
    
    constraints = {
      complexAttribute: {
        equality: {
          attribute: "otherComplexAttribute",
          message: "is not complex enough",
          comparator: function[v1, v2] {
            return JSON.stringify[v1] === JSON.stringify[v2];
          }
        }
      }
    };
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
    // => undefined
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
    // => {complexAttribute: ["Complex attribute is not complex enough"]}
    58 [mảng trống]
  • var constraints = {
      confirmPassword: {
        equality: "password"
      }
    };
    
    validate[{password: "foo", confirmPassword: "foo"}, constraints];
    // => undefined
    
    validate[{password: "foo", confirmPassword: "bar"}, constraints];
    // => {confirmPassword: ["Confirm password is not equal to password"]}
    
    constraints = {
      complexAttribute: {
        equality: {
          attribute: "otherComplexAttribute",
          message: "is not complex enough",
          comparator: function[v1, v2] {
            return JSON.stringify[v1] === JSON.stringify[v2];
          }
        }
      }
    };
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
    // => undefined
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
    // => {complexAttribute: ["Complex attribute is not complex enough"]}
    59 [chuỗi trống]
  • var constraints = {
      confirmPassword: {
        equality: "password"
      }
    };
    
    validate[{password: "foo", confirmPassword: "foo"}, constraints];
    // => undefined
    
    validate[{password: "foo", confirmPassword: "bar"}, constraints];
    // => {confirmPassword: ["Confirm password is not equal to password"]}
    
    constraints = {
      complexAttribute: {
        equality: {
          attribute: "otherComplexAttribute",
          message: "is not complex enough",
          comparator: function[v1, v2] {
            return JSON.stringify[v1] === JSON.stringify[v2];
          }
        }
      }
    };
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
    // => undefined
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
    // => {complexAttribute: ["Complex attribute is not complex enough"]}
    70 [chuỗi chỉ có khoảng trắng]

Quan trọng. Tất cả các giá trị khác được coi là hợp lệ [bao gồm cả chức năng]

var constraints = {
  from: {
    email: true
  }
};

validate[{from: null}, constraints];
// => undefined

validate[{from: ""}, constraints];
// => {"email": ["From is not a valid email"]}

validate[{from: "[email protected]"}, constraints];
// => {"email": ["From is not a valid email"]}

// Any TLD is allowed
validate[{from: "[email protected]"}, constraints];
// => undefined

// Upper cased emails are allowed
validate[{from: "[email protected]"}, constraints];
// => undefined

constraints = {
  from: {
    email: {
      message: "doesn't look like a valid email"
    }
  }
};

validate[{from: "foobar"}, constraints];
// => {"email": ["From doesn't look like a valid email"]}

// It allows unicode
validate[{from: "first.lä[email protected]"}, constraints];
// => undefined
8

Trình xác nhận loại đảm bảo rằng đầu vào là loại chính xác. Có các loại tích hợp sau

  • var constraints = {
      confirmPassword: {
        equality: "password"
      }
    };
    
    validate[{password: "foo", confirmPassword: "foo"}, constraints];
    // => undefined
    
    validate[{password: "foo", confirmPassword: "bar"}, constraints];
    // => {confirmPassword: ["Confirm password is not equal to password"]}
    
    constraints = {
      complexAttribute: {
        equality: {
          attribute: "otherComplexAttribute",
          message: "is not complex enough",
          comparator: function[v1, v2] {
            return JSON.stringify[v1] === JSON.stringify[v2];
          }
        }
      }
    };
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
    // => undefined
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
    // => {complexAttribute: ["Complex attribute is not complex enough"]}
    71
  • var constraints = {
      confirmPassword: {
        equality: "password"
      }
    };
    
    validate[{password: "foo", confirmPassword: "foo"}, constraints];
    // => undefined
    
    validate[{password: "foo", confirmPassword: "bar"}, constraints];
    // => {confirmPassword: ["Confirm password is not equal to password"]}
    
    constraints = {
      complexAttribute: {
        equality: {
          attribute: "otherComplexAttribute",
          message: "is not complex enough",
          comparator: function[v1, v2] {
            return JSON.stringify[v1] === JSON.stringify[v2];
          }
        }
      }
    };
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
    // => undefined
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
    // => {complexAttribute: ["Complex attribute is not complex enough"]}
    72
  • var constraints = {
      confirmPassword: {
        equality: "password"
      }
    };
    
    validate[{password: "foo", confirmPassword: "foo"}, constraints];
    // => undefined
    
    validate[{password: "foo", confirmPassword: "bar"}, constraints];
    // => {confirmPassword: ["Confirm password is not equal to password"]}
    
    constraints = {
      complexAttribute: {
        equality: {
          attribute: "otherComplexAttribute",
          message: "is not complex enough",
          comparator: function[v1, v2] {
            return JSON.stringify[v1] === JSON.stringify[v2];
          }
        }
      }
    };
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
    // => undefined
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
    // => {complexAttribute: ["Complex attribute is not complex enough"]}
    73
  • var constraints = {
      confirmPassword: {
        equality: "password"
      }
    };
    
    validate[{password: "foo", confirmPassword: "foo"}, constraints];
    // => undefined
    
    validate[{password: "foo", confirmPassword: "bar"}, constraints];
    // => {confirmPassword: ["Confirm password is not equal to password"]}
    
    constraints = {
      complexAttribute: {
        equality: {
          attribute: "otherComplexAttribute",
          message: "is not complex enough",
          comparator: function[v1, v2] {
            return JSON.stringify[v1] === JSON.stringify[v2];
          }
        }
      }
    };
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
    // => undefined
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
    // => {complexAttribute: ["Complex attribute is not complex enough"]}
    74
  • var constraints = {
      confirmPassword: {
        equality: "password"
      }
    };
    
    validate[{password: "foo", confirmPassword: "foo"}, constraints];
    // => undefined
    
    validate[{password: "foo", confirmPassword: "bar"}, constraints];
    // => {confirmPassword: ["Confirm password is not equal to password"]}
    
    constraints = {
      complexAttribute: {
        equality: {
          attribute: "otherComplexAttribute",
          message: "is not complex enough",
          comparator: function[v1, v2] {
            return JSON.stringify[v1] === JSON.stringify[v2];
          }
        }
      }
    };
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
    // => undefined
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
    // => {complexAttribute: ["Complex attribute is not complex enough"]}
    75
  • var constraints = {
      confirmPassword: {
        equality: "password"
      }
    };
    
    validate[{password: "foo", confirmPassword: "foo"}, constraints];
    // => undefined
    
    validate[{password: "foo", confirmPassword: "bar"}, constraints];
    // => {confirmPassword: ["Confirm password is not equal to password"]}
    
    constraints = {
      complexAttribute: {
        equality: {
          attribute: "otherComplexAttribute",
          message: "is not complex enough",
          comparator: function[v1, v2] {
            return JSON.stringify[v1] === JSON.stringify[v2];
          }
        }
      }
    };
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
    // => undefined
    
    validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
    // => {complexAttribute: ["Complex attribute is not complex enough"]}
    76

Ngoài những thứ này, bạn cũng có thể tạo của riêng mình bằng cách thêm chúng vào

var constraints = {
  confirmPassword: {
    equality: "password"
  }
};

validate[{password: "foo", confirmPassword: "foo"}, constraints];
// => undefined

validate[{password: "foo", confirmPassword: "bar"}, constraints];
// => {confirmPassword: ["Confirm password is not equal to password"]}

constraints = {
  complexAttribute: {
    equality: {
      attribute: "otherComplexAttribute",
      message: "is not complex enough",
      comparator: function[v1, v2] {
        return JSON.stringify[v1] === JSON.stringify[v2];
      }
    }
  }
};

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
// => undefined

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
// => {complexAttribute: ["Complex attribute is not complex enough"]}
77

Các tùy chọn sau được hỗ trợ

  • loại - Loại để sử dụng. Cũng có thể là một chức năng để kiểm tra loại nội tuyến. Hàm sẽ nhận giá trị, tùy chọn, tên thuộc tính, tất cả các thuộc tính và tùy chọn chung tương ứng
  • tin nhắn - Một tin nhắn tùy chỉnh. Cũng có thể là một chức năng. Hàm sẽ nhận giá trị, tùy chọn, tên thuộc tính, tất cả các thuộc tính và tùy chọn chung tương ứng
var constraints = {
  confirmPassword: {
    equality: "password"
  }
};

validate[{password: "foo", confirmPassword: "foo"}, constraints];
// => undefined

validate[{password: "foo", confirmPassword: "bar"}, constraints];
// => {confirmPassword: ["Confirm password is not equal to password"]}

constraints = {
  complexAttribute: {
    equality: {
      attribute: "otherComplexAttribute",
      message: "is not complex enough",
      comparator: function[v1, v2] {
        return JSON.stringify[v1] === JSON.stringify[v2];
      }
    }
  }
};

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [1,2,3]}, constraints];
// => undefined

validate[{complexAttribute: [1,2,3], otherComplexAttribute: [3,2,1]}, constraints];
// => {complexAttribute: ["Complex attribute is not complex enough"]}
6

Trình xác thực URL đảm bảo rằng đầu vào là một URL hợp lệ. Việc xác thực URL khá phức tạp nhưng trình xác thực này tuân theo ý chính có thể tìm thấy tại đây

Làm cách nào để xác thực mã JavaScript trực tuyến?

Làm cách nào để xác thực mã hoặc tệp JavaScript? .
Mở công cụ Trình xác thực JS và Sao chép và Dán mã JS trong Trình soạn thảo văn bản đầu vào
Nếu bạn có tệp JavaScript, bạn có thể tải tệp lên bằng nút Tải tệp lên. .
Nhấp vào nút Xác thực JS khi dữ liệu tập lệnh js có sẵn trong Trình soạn thảo văn bản, thông qua Dán, Tệp hoặc URL

Làm cách nào để kiểm tra lỗi mã JavaScript?

Nhấn Command+Option+J [Mac] hoặc Control+Shift+J [Windows, Linux, Chrome OS] để chuyển thẳng vào bảng điều khiển của Chrome DevTools . Hoặc, điều hướng đến Công cụ khác > Công cụ dành cho nhà phát triển từ menu Chrome và nhấp vào tab Bảng điều khiển. Bảng điều khiển lỗi sẽ mở ra. Nếu bạn không thấy bất kỳ lỗi nào, hãy thử tải lại trang.

Làm cách nào để xác thực một đối tượng trong JavaScript?

Bản trình diễn trực tiếp. .
hàm is_object[giá trị]
var kiểu dữ liệu = kiểu giá trị;
trả về kiểu dữ liệu === 'chức năng'. kiểu dữ liệu === 'đối tượng' &&. giá trị;
bảng điều khiển. nhật ký [is_object [{tên. 'Robert'}]];

Làm cách nào để sửa mã trong JavaScript?

Vì chúng tôi biết hai loại lỗi JavaScript chính nên chúng tôi có thể nhanh chóng chuyển sang sửa chúng. .
Khi nghi ngờ, hãy tải lại. Đôi khi điều đơn giản nhất chúng ta có thể làm là tải lại trang web. .
Kiểm tra xem JavaScript đã được bật chưa. .
Sử dụng Công cụ dành cho nhà phát triển trong trình duyệt. .
Kiểm tra khả năng tương thích giữa các trình duyệt. .
Sử dụng bảng điều khiển. .
Đến cuối cùng

Chủ Đề