📄 spryvalidationtextfield.js
字号:
}
}
//DELETE
if (this.useCharacterMasking && this.pattern && this.keyCode == 46) {
if (e.ctrlKey) {
//delete from selection until end
this.setValue(this.input.value.substring(0, this.selection.start));
} else if (this.selection.end == this.input.value.length || this.selection.start == this.input.value.length-1){
//allow key if selection is at end (will delete selection)
return true;
} else {
this.flags.operaRevertOnKeyUp = true;
}
if (Spry.is.mozilla && Spry.is.mac) {
this.flags.skp = true;
}
Spry.Widget.Utils.stopEvent(e);
return false;
}
//BACKSPACE
if (this.useCharacterMasking && this.pattern && !e.ctrlKey && this.keyCode == 8) {
if (this.selection.start == this.input.value.length) {
//delete with BACKSPACE from the end of the input value only
var n = this.getAutoComplete(this.selection.start, -1);
this.setValue(this.input.value.substring(0, this.input.value.length - (Spry.is.opera?0:1) - n.length));
if (Spry.is.opera) {
//cant stop the event on Opera, we'll just preserve the selection so delete will act on it
this.selection.start = this.selection.start - 1 - n.length;
this.selection.end = this.selection.end - 1 - n.length;
}
} else if (this.selection.end == this.input.value.length){
//allow BACKSPACE if selection is at end (will delete selection)
return true;
} else {
this.flags.operaRevertOnKeyUp = true;
}
if (Spry.is.mozilla && Spry.is.mac) {
this.flags.skp = true;
}
Spry.Widget.Utils.stopEvent(e);
return false;
}
return true;
};
Spry.Widget.ValidationTextField.prototype.onMouseDown = function(e)
{
if (this.flags.active) {
//mousedown fires before focus
//avoid double saveState on first focus by mousedown by checking if the control has focus
//do nothing if it's not focused because saveState will be called onfocus
this.saveState();
}
};
Spry.Widget.ValidationTextField.prototype.onDrop = function(e)
{
//mark that a drop operation is in progress to avoid race conditions with event handlers for other events
//especially onchange and onfocus
this.flags.drop = true;
this.removeHint();
this.saveState();
this.flags.active = true;
this.addClassName(this.element, this.focusClass);
this.addClassName(this.additionalError, this.focusClass);
};
Spry.Widget.ValidationTextField.prototype.onFocus = function(e)
{
if (this.flags.drop) {
return;
}
this.removeHint();
if (this.pattern && this.useCharacterMasking) {
var autocomplete = this.getAutoComplete(this.selection.start);
this.setValue(this.input.value + autocomplete);
this.selection.moveTo(this.input.value.length, this.input.value.length);
}
this.saveState();
this.flags.active = true;
this.addClassName(this.element, this.focusClass);
this.addClassName(this.additionalError, this.focusClass);
};
Spry.Widget.ValidationTextField.prototype.onBlur = function(e)
{
this.flags.active = false;
this.removeClassName(this.element, this.focusClass);
this.removeClassName(this.additionalError, this.focusClass);
this.flags.restoreSelection = false;
var mustRevert = this.doValidations(this.input.value, this.input.value);
this.flags.restoreSelection = true;
if (this.validateOn & Spry.Widget.ValidationTextField.ONBLUR) {
this.validate();
}
var self = this;
setTimeout(function() {self.putHint();}, 10);
return true;
};
Spry.Widget.ValidationTextField.prototype.compilePattern = function() {
if (!this.pattern) {
return;
}
var compiled = [];
var regexps = [];
var patternCharacters = [];
var idx = 0;
var c = '', p = '';
for (var i=0; i<this.pattern.length; i++) {
c = this.pattern.charAt(i);
if (p == '\\') {
if (/[0ABXY\?]/i.test(c)) {
regexps[idx - 1] = c;
} else {
regexps[idx - 1] = Spry.Widget.ValidationTextField.regExpFromChars(c);
}
compiled[idx - 1] = c;
patternCharacters[idx - 1] = null;
p = '';
continue;
}
regexps[idx] = Spry.Widget.ValidationTextField.regExpFromChars(c);
if (/[0ABXY\?]/i.test(c)) {
compiled[idx] = null;
patternCharacters[idx] = c;
} else if (c == '\\') {
compiled[idx] = c;
patternCharacters[idx] = '\\';
} else {
compiled[idx] = c;
patternCharacters[idx] = null;
}
idx++;
p = c;
}
this.autoCompleteCharacters = compiled;
this.compiledPattern = regexps;
this.patternCharacters = patternCharacters;
this.patternLength = compiled.length;
};
Spry.Widget.ValidationTextField.prototype.getAutoComplete = function(from, direction) {
if (direction == -1) {
var n = '', m = '';
while(from && (n = this.getAutoComplete(--from) )) {
m = n;
}
return m;
}
var ret = '', c = '';
for (var i=from; i<this.autoCompleteCharacters.length; i++) {
c = this.autoCompleteCharacters[i];
if (c) {
ret += c;
} else {
break;
}
}
return ret;
};
Spry.Widget.ValidationTextField.regExpFromChars = function (string) {
//string contains pattern characters
var ret = '', character = '';
for (var i = 0; i<string.length; i++) {
character = string.charAt(i);
switch (character) {
case '0': ret += '\\d';break;
case 'A': ret += '[A-Z]';break;
// case 'A': ret += '[\u0041-\u005A\u0061-\u007A\u0100-\u017E\u0180-\u0233\u0391-\u03CE\u0410-\u044F\u05D0-\u05EA\u0621-\u063A\u0641-\u064A\u0661-\u06D3\u06F1-\u06FE]';break;
case 'a': ret += '[a-z]';break;
// case 'a': ret += '[\u0080-\u00FF]';break;
case 'B': case 'b': ret += '[a-zA-Z]';break;
case 'x': ret += '[0-9a-z]';break;
case 'X': ret += '[0-9A-Z]';break;
case 'Y': case 'y': ret += '[0-9a-zA-Z]';break;
case '?': ret += '.';break;
case '1':case '2':case '3':case '4':case '5':case '6':case '7':case '8':case '9':
ret += character;
break;
case 'c': case 'C': case 'e': case 'E': case 'f': case 'F':case 'r':case 'd': case 'D':case 'n':case 's':case 'S':case 'w':case 'W':case 't':case 'v':
ret += character;
break;
default: ret += '\\' + character;
}
}
return ret;
};
Spry.Widget.ValidationTextField.prototype.patternToRegExp = function(len) {
var ret = '^';
var end = Math.min(this.compiledPattern.length, len);
for (var i=0; i < end; i++) {
ret += this.compiledPattern[i];
}
ret += '$';
ret = new RegExp(ret, "");
return ret;
};
Spry.Widget.ValidationTextField.prototype.resetClasses = function() {
var classes = [this.requiredClass, this.invalidFormatClass, this.invalidRangeMinClass, this.invalidRangeMaxClass, this.invalidCharsMinClass, this.invalidCharsMaxClass, this.validClass];
for (var i=0; i < classes.length; i++)
{
this.removeClassName(this.element, classes[i]);
this.removeClassName(this.additionalError, classes[i]);
}
};
Spry.Widget.ValidationTextField.prototype.reset = function() {
this.removeHint();
this.oldValue = this.input.defaultValue;
this.resetClasses();
if (Spry.is.ie) {
//this will fire the onpropertychange event right after the className changed on the container element
//IE6 will not fire the first onpropertychange on an input type text after a onreset handler if inside that handler the className of one of the elements inside the form has been changed
//to reproduce: change the className of one of the elements inside the form from within the onreset handler; then the onpropertychange does not fire the first time
this.input.forceFireFirstOnPropertyChange = true;
this.input.removeAttribute("forceFireFirstOnPropertyChange");
}
var self = this;
setTimeout(function() {self.putHint();}, 10);
};
Spry.Widget.ValidationTextField.prototype.validate = function() {
this.resetClasses();
//possible states: required, format, rangeMin, rangeMax, charsMin, charsMax
if (this.validateOn & Spry.Widget.ValidationTextField.ONSUBMIT) {
this.removeHint();
this.doValidations(this.input.value, this.input.value);
if(!this.flags.active) {
var self = this;
setTimeout(function() {self.putHint();}, 10);
}
}
if (this.isRequired && this.errors & Spry.Widget.ValidationTextField.ERROR_REQUIRED) {
this.addClassName(this.element, this.requiredClass);
this.addClassName(this.additionalError, this.requiredClass);
return false;
}
if (this.errors & Spry.Widget.ValidationTextField.ERROR_FORMAT) {
this.addClassName(this.element, this.invalidFormatClass);
this.addClassName(this.additionalError, this.invalidFormatClass);
return false;
}
if (this.errors & Spry.Widget.ValidationTextField.ERROR_RANGE_MIN) {
this.addClassName(this.element, this.invalidRangeMinClass);
this.addClassName(this.additionalError, this.invalidRangeMinClass);
return false;
}
if (this.errors & Spry.Widget.ValidationTextField.ERROR_RANGE_MAX) {
this.addClassName(this.element, this.invalidRangeMaxClass);
this.addClassName(this.additionalError, this.invalidRangeMaxClass);
return false;
}
if (this.errors & Spry.Widget.ValidationTextField.ERROR_CHARS_MIN) {
this.addClassName(this.element, this.invalidCharsMinClass);
this.addClassName(this.additionalError, this.invalidCharsMinClass);
return false;
}
if (this.errors & Spry.Widget.ValidationTextField.ERROR_CHARS_MAX) {
this.addClassName(this.element, this.invalidCharsMaxClass);
this.addClassName(this.additionalError, this.invalidCharsMaxClass);
return false;
}
this.addClassName(this.element, this.validClass);
this.addClassName(this.additionalError, this.validClass);
return true;
};
Spry.Widget.ValidationTextField.prototype.addClassName = function(ele, className)
{
if (!ele || !className || (ele.className && ele.className.search(new RegExp("\\b" + className + "\\b")) != -1))
return;
ele.className += (ele.className ? " " : "") + className;
};
Spry.Widget.ValidationTextField.prototype.removeClassName = function(ele, className)
{
if (!ele || !className || (ele.className && ele.className.search(new RegExp("\\b" + className + "\\b")) == -1))
return;
ele.className = ele.className.replace(new RegExp("\\s*\\b" + className + "\\b", "g"), "");
};
/**
* SelectionDescriptor is a wrapper for input type text selection methods and properties
* as implemented by various browsers
*/
Spry.Widget.SelectionDescriptor = function (element)
{
this.element = element;
this.update();
};
Spry.Widget.SelectionDescriptor.prototype.update = function()
{
if (Spry.is.ie && Spry.is.windows) {
var sel = this.element.ownerDocument.selection;
if (this.element.nodeName == "TEXTAREA") {
if (sel.type != 'None') {
try{var range = sel.createRange();}catch(err){return;}
if (range.parentElement() == this.element){
var range_all = this.element.ownerDocument.body.createTextRange();
range_all.moveToElementText(this.element);
for (var sel_start = 0; range_all.compareEndPoints('StartToStart', range) < 0; sel_start ++){
range_all.moveStart('character', 1);
}
this.start = sel_start;
// create a selection of the whole this.element
range_all = this.element.ownerDocument.body.createTextRange();
range_all.moveToElementText(this.element);
for (var sel_end = 0; range_all.compareEndPoints('StartToEnd', range) < 0; sel_end++){
range_all.moveStart('character', 1);
}
this.end = sel_end;
this.length = this.end - this.start;
// get selected and surrounding text
this.text = range.text;
}
}
} else if (this.element.nodeName == "INPUT"){
try{this.range = sel.createRange();}catch(err){return;}
this.length = this.range.text.length;
var clone = this.range.duplicate();
this.start = -clone.moveStart("character", -10000);
clone = this.range.duplicate();
clone.collapse(false);
this.end = -clone.moveStart("character", -10000);
this.text = this.range.text;
}
} else {
var tmp = this.element;
var selectionStart = 0;
var selectionEnd = 0;
try { selectionStart = tmp.selectionStart;} catch(err) {}
try { selectionEnd = tmp.selectionEnd;} catch(err) {}
if (Spry.is.safari) {
if (selectionStart == 2147483647) {
selectionStart = 0;
}
if (selectionEnd == 2147483647) {
selectionEnd = 0;
}
}
this.start = selectionStart;
this.end = selectionEnd;
this.length = selectionEnd - selectionStart;
this.text = this.element.value.substring(selectionStart, selectionEnd);
}
};
Spry.Widget.SelectionDescriptor.prototype.destroy = function() {
try { delete this.range} catch(err) {}
try { delete this.element} catch(err) {}
};
Spry.W
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -