EdgeExpressDB
view release on metacpan or search on metacpan
www/edgeexpress/jscript/SpryAssets/SpryValidationTextField.js view on Meta::CPAN
var ret = false;
for (var i=0; i<validAddresses.length; i++) {
if (validAddresses[i].test(value)) {
ret = true;
break;
}
}
if (ret && value.indexOf(".") != -1) {
//if address contains IPv4 fragment, it must be valid; all 4 groups must be less than 256
var ipv4 = value.match(/:?(?:\d{1,3}\.){3}\d{1,3}/i);
if(!ipv4) {
return false;
}
ipv4 = ipv4[0].replace(/^:/, '');
var pieces = ipv4.split('.');
if (pieces.length != 4) {
return false;
}
var regExp = /^[\-\+]?\d*$/;
for (var i=0; i< pieces.length; i++) {
if (pieces[i] == '') {
return false;
}
var piece = parseInt(pieces[i], 10);
if (isNaN(piece) || piece > 255 || !regExp.test(pieces[i]) || pieces[i].length>3 || /^0{2,3}$/.test(pieces[i])) {
return false;
}
}
}
if (ret && value.indexOf("/") != -1) {
// if prefix-length is specified must be in [1-128]
var prefLen = value.match(/\/\d{1,3}$/);
if (!prefLen) return false;
var prefLenVal = parseInt(prefLen[0].replace(/^\//,''), 10);
if (isNaN(prefLenVal) || prefLenVal > 128 || prefLenVal < 1) {
return false;
}
}
return ret;
};
Spry.Widget.ValidationTextField.onloadDidFire = false;
Spry.Widget.ValidationTextField.loadQueue = [];
Spry.Widget.ValidationTextField.prototype.isBrowserSupported = function()
{
return Spry.is.ie && Spry.is.v >= 5 && Spry.is.windows
||
Spry.is.mozilla && Spry.is.v >= 1.4
||
Spry.is.safari
||
Spry.is.opera && Spry.is.v >= 9;
};
Spry.Widget.ValidationTextField.prototype.init = function(element, options)
{
this.element = this.getElement(element);
this.errors = 0;
this.flags = {locked: false};
this.options = {};
this.event_handlers = [];
this.validClass = "textfieldValidState";
this.focusClass = "textfieldFocusState";
this.requiredClass = "textfieldRequiredState";
this.invalidFormatClass = "textfieldInvalidFormatState";
this.invalidRangeMinClass = "textfieldMinValueState";
this.invalidRangeMaxClass = "textfieldMaxValueState";
this.invalidCharsMinClass = "textfieldMinCharsState";
this.invalidCharsMaxClass = "textfieldMaxCharsState";
this.textfieldFlashTextClass = "textfieldFlashText";
if (Spry.is.safari) {
this.flags.lastKeyPressedTimeStamp = 0;
}
switch (this.type) {
case 'phone_number':options.format = Spry.Widget.Utils.firstValid(options.format, 'phone_us');break;
case 'currency':options.format = Spry.Widget.Utils.firstValid(options.format, 'comma_dot');break;
case 'zip_code':options.format = Spry.Widget.Utils.firstValid(options.format, 'zip_us5');break;
case 'date':
options.format = Spry.Widget.Utils.firstValid(options.format, 'mm/dd/yy');
break;
case 'time':
options.format = Spry.Widget.Utils.firstValid(options.format, 'HH:mm');
options.pattern = options.format.replace(/[hms]/gi, "0").replace(/TT/gi, 'AM').replace(/T/gi, 'A');
break;
case 'ip':
options.format = Spry.Widget.Utils.firstValid(options.format, 'ipv4');
options.characterMasking = Spry.Widget.ValidationTextField.ValidationDescriptors[this.type].characterMaskingFormats[options.format];
break;
}
//retrieve the validation type descriptor to be used with this instance (base on type and format)
//widgets may have different validations depending on format (like zip_code with formats)
var validationDescriptor = {};
if (options.format && Spry.Widget.ValidationTextField.ValidationDescriptors[this.type].formats) {
if (Spry.Widget.ValidationTextField.ValidationDescriptors[this.type].formats[options.format]) {
Spry.Widget.Utils.setOptions(validationDescriptor, Spry.Widget.ValidationTextField.ValidationDescriptors[this.type].formats[options.format]);
}
} else {
Spry.Widget.Utils.setOptions(validationDescriptor, Spry.Widget.ValidationTextField.ValidationDescriptors[this.type]);
}
//set default values for some parameters which were not aspecified
options.useCharacterMasking = Spry.Widget.Utils.firstValid(options.useCharacterMasking, false);
options.hint = Spry.Widget.Utils.firstValid(options.hint, '');
options.isRequired = Spry.Widget.Utils.firstValid(options.isRequired, true);
//set widget validation parameters
//get values from validation type descriptor
//use the user specified values, if defined
options.characterMasking = Spry.Widget.Utils.firstValid(options.characterMasking, validationDescriptor.characterMasking);
options.regExpFilter = Spry.Widget.Utils.firstValid(options.regExpFilter, validationDescriptor.regExpFilter);
options.pattern = Spry.Widget.Utils.firstValid(options.pattern, validationDescriptor.pattern);
options.validation = Spry.Widget.Utils.firstValid(options.validation, validationDescriptor.validation);
if (typeof options.validation == 'string') {
options.validation = eval(options.validation);
}
www/edgeexpress/jscript/SpryAssets/SpryValidationTextField.js view on Meta::CPAN
}
for (var i=0; i<this.event_handlers.length; i++) {
Spry.Widget.Utils.addEventListener(this.event_handlers[i][0], this.event_handlers[i][1], this.event_handlers[i][2], false);
}
// submit
this.form = Spry.Widget.Utils.getFirstParentWithNodeName(this.input, "FORM");
if (this.form) {
// if no "onSubmit" handler has been attached to the current form, attach one
if (!this.form.attachedSubmitHandler && !this.form.onsubmit) {
this.form.onsubmit = function(e) { e = e || event; return Spry.Widget.Form.onSubmit(e, e.srcElement || e.currentTarget) };
this.form.attachedSubmitHandler = true;
}
if (!this.form.attachedResetHandler) {
Spry.Widget.Utils.addEventListener(this.form, "reset", function(e) { e = e || event; return Spry.Widget.Form.onReset(e, e.srcElement || e.currentTarget) }, false);
this.form.attachedResetHandler = true;
}
// add the currrent widget to the "onSubmit" check queue;
Spry.Widget.Form.onSubmitWidgetQueue.push(this);
}
}
};
Spry.Widget.ValidationTextField.prototype.isDisabled = function() {
return this.input && (this.input.disabled || this.input.readOnly) || !this.input;
};
Spry.Widget.ValidationTextField.prototype.getElement = function(ele)
{
if (ele && typeof ele == "string")
return document.getElementById(ele);
return ele;
};
Spry.Widget.ValidationTextField.addLoadListener = function(handler)
{
if (typeof window.addEventListener != 'undefined')
window.addEventListener('load', handler, false);
else if (typeof document.addEventListener != 'undefined')
document.addEventListener('load', handler, false);
else if (typeof window.attachEvent != 'undefined')
window.attachEvent('onload', handler);
};
Spry.Widget.ValidationTextField.processLoadQueue = function(handler)
{
Spry.Widget.ValidationTextField.onloadDidFire = true;
var q = Spry.Widget.ValidationTextField.loadQueue;
var qlen = q.length;
for (var i = 0; i < qlen; i++)
q[i].attachBehaviors();
};
Spry.Widget.ValidationTextField.addLoadListener(Spry.Widget.ValidationTextField.processLoadQueue);
Spry.Widget.ValidationTextField.addLoadListener(function(){
Spry.Widget.Utils.addEventListener(window, "unload", Spry.Widget.Form.destroyAll, false);
});
Spry.Widget.ValidationTextField.prototype.setValue = function(newValue) {
this.flags.locked = true;
this.input.value = newValue;
this.flags.locked = false;
this.oldValue = newValue;
if (!Spry.is.ie) {
this.onChange();
}
};
/**
* save the state of the input (selection and value) so we can revert to it
* should call this just before modifying the input value
*/
Spry.Widget.ValidationTextField.prototype.saveState = function()
{
this.oldValue = this.input.value;
this.selection.update();
};
Spry.Widget.ValidationTextField.prototype.revertState = function(revertValue)
{
if (revertValue != this.input.value) {
this.input.readOnly = true;
this.input.value = revertValue;
this.input.readOnly = false;
if (Spry.is.safari && this.flags.active) {
this.input.focus();
}
}
this.selection.moveTo(this.selection.start, this.selection.end);
this.redTextFlash();
};
Spry.Widget.ValidationTextField.prototype.removeHint = function()
{
if (this.flags.hintOn) {
this.input.value = "";
this.flags.hintOn = false;
}
};
Spry.Widget.ValidationTextField.prototype.putHint = function()
{
if(this.hint && this.input && this.input.type == "text" && this.input.value == "") {
this.flags.hintOn = true;
this.input.value = this.hint;
}
};
Spry.Widget.ValidationTextField.prototype.redTextFlash = function()
{
var self = this;
this.addClassName(this.element, this.textfieldFlashTextClass);
setTimeout(function() {
self.removeClassName(self.element, self.textfieldFlashTextClass)
}, 100);
};
Spry.Widget.ValidationTextField.prototype.doValidations = function(testValue, revertValue)
{
if (this.isDisabled()) return false;
if (this.flags.locked) {
return false;
}
if (testValue.length == 0 && !this.isRequired) {
this.errors = 0;
return false;
}
this.flags.locked = true;
var mustRevert = false;
var continueValidations = true;
if (!this.options.isRequired && testValue.length == 0) {
continueValidations = false;
}
var errors = 0;
var fixedValue = testValue;
//characterMasking - test if all characters are valid with the characterMasking (keyboard filter)
if (this.useCharacterMasking && this.characterMasking) {
for(var i=0; i<testValue.length; i++) {
if (!this.characterMasking.test(testValue.charAt(i))) {
errors = errors | Spry.Widget.ValidationTextField.ERROR_FORMAT;
fixedValue = revertValue;
mustRevert = true;
break;
}
}
}
//regExpFilter - character mask positioning (additional mask to restrict some characters only in some position)
if (!mustRevert && this.useCharacterMasking && this.regExpFilter) {
if (!this.regExpFilter.test(fixedValue)) {
errors = errors | Spry.Widget.ValidationTextField.ERROR_FORMAT;
mustRevert = true;
}
}
//pattern - testValue matches the pattern so far
if (!mustRevert && this.pattern) {
var currentRegExp = this.patternToRegExp(testValue.length);
if (!currentRegExp.test(testValue)) {
errors = errors | Spry.Widget.ValidationTextField.ERROR_FORMAT;
mustRevert = true;
} else if (this.patternLength != testValue.length) {
//testValue matches pattern so far, but it's not ok if it does not have the proper length
//do not revert, but should show the error
errors = errors | Spry.Widget.ValidationTextField.ERROR_FORMAT;
}
}
if (fixedValue == '') {
errors = errors | Spry.Widget.ValidationTextField.ERROR_REQUIRED;
}
if (!mustRevert && this.pattern && this.useCharacterMasking) {
var n = this.getAutoComplete(testValue.length);
if (n) {
fixedValue += n;
}
}
if(!mustRevert && this.minChars !== null && continueValidations) {
if (testValue.length < this.minChars) {
errors = errors | Spry.Widget.ValidationTextField.ERROR_CHARS_MIN;
continueValidations = false;
}
}
if(!mustRevert && this.maxChars !== null && continueValidations) {
if (testValue.length > this.maxChars) {
errors = errors | Spry.Widget.ValidationTextField.ERROR_CHARS_MAX;
continueValidations = false;
}
}
//validation - testValue passes widget validation function
if (!mustRevert && this.validation && continueValidations) {
var value = this.validation(fixedValue, this.options);
if (false === value) {
errors = errors | Spry.Widget.ValidationTextField.ERROR_FORMAT;
continueValidations = false;
} else {
this.typedValue = value;
}
}
if(!mustRevert && this.validation && this.minValue !== null && continueValidations) {
var minValue = this.validation(this.minValue, this.options);
if (minValue !== false) {
if (this.typedValue < minValue) {
errors = errors | Spry.Widget.ValidationTextField.ERROR_RANGE_MIN;
continueValidations = false;
}
}
}
if(!mustRevert && this.validation && this.maxValue !== null && continueValidations) {
var maxValue = this.validation(this.maxValue, this.options);
if (maxValue !== false) {
if( this.typedValue > maxValue) {
errors = errors | Spry.Widget.ValidationTextField.ERROR_RANGE_MAX;
continueValidations = false;
}
}
}
//an invalid value was tested; must make sure it does not get inside the input
if (this.useCharacterMasking && mustRevert) {
this.revertState(revertValue);
}
this.errors = errors;
this.fixedValue = fixedValue;
this.flags.locked = false;
return mustRevert;
};
Spry.Widget.ValidationTextField.prototype.onChange = function(e)
{
if (Spry.is.opera && this.flags.operaRevertOnKeyUp) {
return true;
}
if (Spry.is.ie && e && e.propertyName != 'value') {
return true;
}
if (this.flags.drop) {
//delay this if it's a drop operation
var self = this;
setTimeout(function() {
self.flags.drop = false;
self.onChange(null);
}, 0);
return;
}
if (this.flags.hintOn) {
return true;
}
if (this.keyCode == 8 || this.keyCode == 46 ) {
var mustRevert = this.doValidations(this.input.value, this.input.value);
this.oldValue = this.input.value;
if ((mustRevert || this.errors) && this.validateOn & Spry.Widget.ValidationTextField.ONCHANGE) {
var self = this;
setTimeout(function() {self.validate();}, 0);
return true;
}
}
var mustRevert = this.doValidations(this.input.value, this.oldValue);
if ((!mustRevert || this.errors) && this.validateOn & Spry.Widget.ValidationTextField.ONCHANGE) {
var self = this;
setTimeout(function() {self.validate();}, 0);
}
return true;
};
Spry.Widget.ValidationTextField.prototype.onKeyUp = function(e) {
if (this.flags.operaRevertOnKeyUp) {
this.setValue(this.oldValue);
Spry.Widget.Utils.stopEvent(e);
this.selection.moveTo(this.selection.start, this.selection.start);
this.flags.operaRevertOnKeyUp = false;
return false;
}
if (this.flags.operaPasteOperation) {
window.clearInterval(this.flags.operaPasteOperation);
this.flags.operaPasteOperation = null;
}
};
Spry.Widget.ValidationTextField.prototype.operaPasteMonitor = function() {
( run in 0.769 second using v1.01-cache-2.11-cpan-39bf76dae61 )