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ốiXá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 conTin 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"]}
3var 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 nhauThô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"]}
7var 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"]}
8var 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"]}
1Trì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"]}
9Tù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"]}
3Trì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"]}
10var 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"]}
5Trì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ạnCá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
14var 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"]}
15var 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"]}
16var 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"]}
17var 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"]}
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"]}
30Mộ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"]}
7Trì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àyCó 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ẵnNế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
14var 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"]}
35var 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"]}
36var 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"]}
37var 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"]}
38var 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"]}
39var 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"]}
50var 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"]}
51var 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"]}
52var 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"]}
53var 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"]}
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
0Trì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
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]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"]}
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
8Trì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
71var 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"]}
72var 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"]}
73var 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"]}
74var 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"]}
75var 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"]}
76var 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"]}
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"]}
77Cá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"]}
6Trì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