| 1 |
(function( $ ) { |
| 2 |
|
| 3 |
$.fn.wppStatelessValidate = function(options, response) { |
| 4 |
|
| 5 |
var _this = jQuery(this); |
| 6 |
|
| 7 |
if (typeof _this.data('settings') == 'undefined'){ |
| 8 |
var _settings = $.extend({ |
| 9 |
name: {}, |
| 10 |
id: {}, |
| 11 |
}, options ); |
| 12 |
|
| 13 |
_this.data('settings', _settings); |
| 14 |
} |
| 15 |
|
| 16 |
var pName = _this.val(); |
| 17 |
var settings = _settings || _this.data('settings'); |
| 18 |
if(pName){ |
| 19 |
pName = pName.trim().replace(/-$/, ''); |
| 20 |
} |
| 21 |
response = response || {}; |
| 22 |
|
| 23 |
response.id = ''; |
| 24 |
response.pName = pName; |
| 25 |
response.success = true; |
| 26 |
response.existing = false; |
| 27 |
response.message = ''; |
| 28 |
|
| 29 |
var isExisting = /(.+)\((.+)\)/.exec(pName); |
| 30 |
|
| 31 |
if(isExisting != null && isExisting.length){ |
| 32 |
response.id = isExisting[2].trim(); |
| 33 |
response.pName = isExisting[1].trim(); |
| 34 |
pName = response.pName; |
| 35 |
} |
| 36 |
|
| 37 |
if(_this.wpStatelessComboBox({has: response.id || response.pName})){ |
| 38 |
response.existing = true; |
| 39 |
return response; |
| 40 |
} |
| 41 |
|
| 42 |
jQuery.each(settings.name, function(index, item) { |
| 43 |
if(!item.regex.test(pName)){ |
| 44 |
response.success = false; |
| 45 |
response.message += item.errorMessage + '<br />'; |
| 46 |
if(typeof item.break != 'undefined' && item.break == true) |
| 47 |
return false; |
| 48 |
} |
| 49 |
}); |
| 50 |
|
| 51 |
if(response.success == false || typeof settings.id == 'undefined' || typeof settings.id.regex == 'undefined'){ |
| 52 |
return response; |
| 53 |
} |
| 54 |
|
| 55 |
var _id = settings.id.regex.exec(pName.toLowerCase()); |
| 56 |
if(_id && typeof _id[0] != 'undefined'){ |
| 57 |
response.id = _id[0]; |
| 58 |
} |
| 59 |
|
| 60 |
jQuery.each(settings.id.replace, function(index, array) { |
| 61 |
var search = array[1]; |
| 62 |
var replace = array[0]; |
| 63 |
response.id = response.id.replace(search, replace); |
| 64 |
}); |
| 65 |
|
| 66 |
response.id = response.id.slice(0, 23) + '-' + Math.floor((Math.random() * 1000000)); |
| 67 |
|
| 68 |
return response; |
| 69 |
|
| 70 |
}; |
| 71 |
|
| 72 |
}( jQuery )); |
| 73 |
|