-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation-without.html
More file actions
37 lines (37 loc) · 938 Bytes
/
validation-without.html
File metadata and controls
37 lines (37 loc) · 938 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script src="jquery-1.11.2.js"></script>
<script src="underscore.js"></script>
<script src="backbone.js"></script>
<script>
var Man = Backbone.Model.extend({
initialize : function(){
this.on("invalid",function(model,error){
alert(error);
});
},
validate : function(attrs, options){
if (attrs.age < 18){
return 'below 18';
}
}
});
//Object will be created with invalid attribute 'age'
var man = new Man({name : 'qian', age : 12});
console.log(man) // Returns an object with invalid attributes
// But we'll use only valid objects.
// Also we'll get the error message in alert, if validation fails.
if(man.isValid()){
alert( man.get('name') );
}
var man = new Man({name : 'qian', age : 19});
if(man.isValid()){
alert( man.get('name') );
</script>
</body>
</html>