﻿// custom validation method - could this be done as regex?
jQuery.validator.addMethod("namelist", function(value, element) { 
    var parts = value.split(",");
    for ( var i=0; i<parts.length; ++i ) {
        var part = parts[i].trim();
        if(part == "")
            continue;
        var names = part.split(" ");
        if((names.length<2) || (names[names.length-1].substr(1)=="."))
            return false;
    }        
    return true;
}, jQuery.validator.format("Please enter a comma-separated list of [forename] [surname].")); 

jQuery.validator.addMethod("isbn", function(value, element) { 
	return this.optional(element) || /^[0-9\- ]*$/i.test(value);
//	return this.optional(element) || /^(?=.{13}$)\d{1,5}([- ])\d{1,7}\1\d{1,6}\1(\d|X)$/i.test(value);
}, jQuery.validator.format("Entry is not an ISBN.")); 

jQuery.validator.addMethod("issn", function(value, element) { 
	return this.optional(element) || /^\d{4}-\d{3}(\d|X)$/i.test(value);
}, jQuery.validator.format("Entry is not an ISSN.")); 

jQuery.validator.addMethod("doi", function(value, element) { 
	return this.optional(element) || /^(10\.\d+\/[^/^>^<]+)$/i.test(value);
}, jQuery.validator.format("Entry is not an DOI.")); 

jQuery.validator.addMethod("one", function(value, element) { 
	return (element.value != "0");
}, jQuery.validator.format("Please select an option.")); 

// multiSelect is set by javascript so we rely on the different element.value to distinguish between zero_or_more and one_or_more
jQuery.validator.addMethod("multiSelect", function(value, element) { 
	return (element.value != "Select one or more options");
}, jQuery.validator.format("Please select at least one option.")); 

jQuery.validator.addMethod("dateGB", function(value, element) { 
	return this.optional(element) || /^[0-3][0-9]\/[0-1][0-9]\/[1-2][0-9][0-9][0-9]$/i.test(value);
}, jQuery.validator.format("Entry is not a valid date (dd/mm/yyyy).")); 

jQuery.validator.addMethod("folio", function(value, element) { 
	return this.optional(element) || /^([IE][A-Z0-9]{1,5}|N\d{1,5}-\d{2})?$/i.test(value);
}, jQuery.validator.format("Entry is not a valid folio.")); 

jQuery.validator.addMethod("money", function(value, element) { 
	return this.optional(element) || /^(£|\$|€)[0-9,]+$/i.test(value);
}, jQuery.validator.format("Entry is not a valid currency amount.")); 

jQuery.validator.addMethod("internal", function(value, element) { 
	return this.optional(element) || /^[a-z0-9_]+$/i.test(value);
}, jQuery.validator.format("Entry can only contain lower case alphanumeric characters and underscore.")); 

jQuery.validator.addMethod("nofuture", function(value, element) { 
    var myDate = new Date(value.substr(3,2)+"/"+value.substr(0,2)+"/"+value.substr(6,4));
    var today = new Date();        
	return this.optional(element) || ( myDate<=today);
}, jQuery.validator.format("Date must not be in the future.")); 

jQuery.validator.addMethod("linkedin", function(value, element) { 
	return this.optional(element) || /^[a-zA-Z0-9]+$/i.test(value);
}, jQuery.validator.format("Entry is not a valid linkedin id.")); 

jQuery.validator.addMethod("twitter", function(value, element) { 
	return this.optional(element) || /^@[a-zA-Z0-9]+$/i.test(value);
}, jQuery.validator.format("Entry is not a valid twitter id.")); 

