CodesJava

Easy learning with example program codes

Backbone.JS ValidationError()

The Backbone.JS ValidationError() model is used to display an error, in case if a validation fails or after an invalid event is triggered.

Syntax:

Model.ValidationError 

Example:

<!DOCTYPE html> 
<html>
<head>  
<title>Example</title>  
<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>  
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js" 
type="text/javascript"></script>  
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js" 
type="text/javascript"></script>  
</head>  
<body>  
<script type="text/javascript">  
var X = Backbone.Model.extend({  
defaults: {  
student_name: 'Tom',  
student_age: 10,  
},  
initialize : function(){  
this.on("invalid",function(model,error){  
document.write(error);  
});  
},  
validate: function(attributes) {  
if ( attributes.student_age < 10 ) {  
return 'Enter the correct age.';  
}  
},  
});  
var Y = new X();  
Y.on('invalid', function() {  
this.arguments;  
});  
Y.set({ student_age : '9' }, { validate : true });  
</script>  
</body>  
</html>

Output:

Enter the correct age.

Explanation:
Since we set the value of student_age less than 10, hence it returned a Validation Error message.

Please follow and like us:
error









Copyright © 2019 CodesJava DMCA.com Protection Status