📄 spryvalidationtextfield.js
字号:
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);
};
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);
};
Spry.Widget.ValidationTextField.prototype.onBlur = function(e)
{
this.flags.active = false;
this.removeClassName(this.element, this.focusClass);
var mustRevert = this.doValidations(this.input.value, this.input.value);
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.reset = function() {
this.removeHint();
this.oldValue = this.input.defaultValue;
this.removeClassName(this.element, this.requiredClass);
this.removeClassName(this.element, this.invalidFormatClass);
this.removeClassName(this.element, this.invalidRangeMinClass);
this.removeClassName(this.element, this.invalidRangeMaxClass);
this.removeClassName(this.element, this.invalidCharsMinClass);
this.removeClassName(this.element, this.invalidCharsMaxClass);
this.removeClassName(this.element, this.validClass);
var self = this;
setTimeout(function() {self.putHint();}, 10);
};
Spry.Widget.ValidationTextField.prototype.validate = function() {
this.removeClassName(this.element, this.requiredClass);
this.removeClassName(this.element, this.invalidFormatClass);
this.removeClassName(this.element, this.invalidRangeMinClass);
this.removeClassName(this.element, this.invalidRangeMaxClass);
this.removeClassName(this.element, this.invalidCharsMinClass);
this.removeClassName(this.element, this.invalidCharsMaxClass);
this.removeClassName(this.element, this.validClass);
//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);
return false;
}
if (this.errors & Spry.Widget.ValidationTextField.ERROR_FORMAT) {
this.addClassName(this.element, this.invalidFormatClass);
return false;
}
if (this.errors & Spry.Widget.ValidationTextField.ERROR_RANGE_MIN) {
this.addClassName(this.element, this.invalidRangeMinClass);
return false;
}
if (this.errors & Spry.Widget.ValidationTextField.ERROR_RANGE_MAX) {
this.addClassName(this.element, this.invalidRangeMaxClass);
return false;
}
if (this.errors & Spry.Widget.ValidationTextField.ERROR_CHARS_MIN) {
this.addClassName(this.element, this.invalidCharsMinClass);
return false;
}
if (this.errors & Spry.Widget.ValidationTextField.ERROR_CHARS_MAX) {
this.addClassName(this.element, this.invalidCharsMaxClass);
return false;
}
this.addClassName(this.element, 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) {
if (this.element.nodeName == "TEXTAREA") {
var range = this.element.ownerDocument.selection.createRange();
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"){
this.range = this.element.ownerDocument.selection.createRange();
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.Widget.SelectionDescriptor.prototype.move = function(amount)
{
if (Spry.is.ie && Spry.is.windows) {
this.range.move("character", amount);
this.range.select();
} else {
try { this.element.selectionStart++;}catch(err) {}
}
this.update();
};
Spry.Widget.SelectionDescriptor.prototype.moveTo = function(start, end)
{
if (Spry.is.ie && Spry.is.windows) {
if (this.element.nodeName == "TEXTAREA") {
var ta_range = this.element.createTextRange();
this.range = this.element.createTextRange();
this.range.move("character", start);
this.range.moveEnd("character", end - start);
var c1 = this.range.compareEndPoints("StartToStart", ta_range);
if (c1 < 0) {
this.range.setEndPoint("StartToStart", ta_range);
}
var c2 = this.range.compareEndPoints("EndToEnd", ta_range);
if (c2 > 0) {
this.range.setEndPoint("EndToEnd", ta_range);
}
} else if (this.element.nodeName == "INPUT"){
this.range = this.element.ownerDocument.selection.createRange();
this.range.move("character", -10000);
this.start = this.range.moveStart("character", start);
this.end = this.start + this.range.moveEnd("character", end - start);
}
this.range.select();
} else {
this.start = start;
try { this.element.selectionStart = start;} catch(err) {}
this.end = end;
try { this.element.selectionEnd = end;} catch(err) {}
}
this.ignore = true;
this.update();
};
Spry.Widget.SelectionDescriptor.prototype.moveEnd = function(amount)
{
if (Spry.is.ie && Spry.is.windows) {
this.range.moveEnd("character", amount);
this.range.select();
} else {
try { this.element.selectionEnd++;} catch(err) {}
}
this.update();
};
Spry.Widge
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -