⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 validators.js

📁 javascript 很酷的类库
💻 JS
📖 第 1 页 / 共 4 页
字号:
        	return  !required || (value != null && !isc.is.emptyString(value));        },                //>@classAttr ValidatorTypes.matchesField (validatorDefinition : object : IR)        //  Tests whether the value for this field matches the value of some other field.        //  The field to compare against is specified via the <code>otherField</code> property        //  on the validator object (should be set to a field name).<br>        //  Note this validator type is only supported for items being edited within a         //  DynamicForm - it cannot be applied to a +link{ListGridField, ListGrid field}.        // @visibility external        // @example matchValue        //<                matchesField : function (item, validator, value) {            // This will happen if the developer specifies a matches field validator on             // a ListGrid field.            if (!item.form) {                this.logWarn("Validator of type 'matchesField' specified for a field with no " +                             "associated DynamicForm instance. Ignoring");                return true;            }        	// get the value of the other field        	var otherFieldValue = item.form.getValue(validator.otherField);        	// and return whether the values match        	return (value == otherFieldValue);        },                //>@classAttr ValidatorTypes.isOneOf (validatorDefinition : object : IR)        // Tests whether the value for this field matches any value from an arbitrary        // list of acceptable values.  The set of acceptable values is specified via        // the <code>list</code> property on the validator, which should be set to an array of        // values. If validator.list is not supplied, the valueMap for the field will be used.        // If there is no valueMap, not providing validator.list is an error.        // @visibility external        //<        isOneOf : function (item, validator, value) {        	// skip empty fields        	if (value == null || isc.is.emptyString(value)) return true;        	// get the list of items to match against, either declared on this validator            // or automatically derived from the field's valueMap (item.valueMap)                        var valueMap = validator.list || (item ? (item.getValueMap ? item.getValueMap()                                                                        : item.valueMap)                                                    : null),                valueList = valueMap;            if (!isc.isAn.Array(valueMap) && isc.isAn.Object(valueMap)) {                valueList = isc.getKeys(valueMap);            }                        if (valueList != null) {                	// if any item == the value, return true            	for (var i = 0, length = valueList.length; i < length; i++) {            		if (valueList[i] == value) return true;            	}            //>DEBUG            } else {                isc.Log.logWarn("isOneOf validator specified with no specified list of options " +                            "or valueMap - validator will always fail. " +                            "Field definition:" + isc.Log.echo(item), "validation");            //<DEBUG            }        	// otherwise, failure return false            if (!validator.errorMessage) {                validator.defaultErrorMessage = isc.Validator.notOneOf;            }        	return false;        },                // Integer validators        // ------------------------------------------------------------------------------------        //>@classAttr ValidatorTypes.integerRange (validatorDefinition : object : IR)        //  Tests whether the value for this field is a whole number within the range         //  specified.  The <code>max</code> and <code>min</code> properties on the validator        //  are used to determine the acceptable range.        // @visibility external        // @example validationBuiltins        //<        integerRange : function (item, validator, value) {            // If we're passed a non numeric value, just return without adding an error.            // This is appropriate since the type of the field will probably be specified as             // "integer" meaning that the built in integer validator will also be present on the            // field.            var passedVal = value;            if (!isc.isA.String(value)) value = parseInt(value);            if (isNaN(value) || value != passedVal) return true;                        // Allow dynamic error messages to be eval'd, with pointers to min and max values            validator.dynamicErrorMessageArguments = {validator:validator,                                                       max:validator.max,                                                       min:validator.min}                	// if a maximum was specified, return false if we're greater than the max        	if (isc.isA.Number(validator.max) && value > validator.max) {        		if (!validator.errorMessage) {        			validator.defaultErrorMessage = isc.Validator.mustBeLessThan        		}        		return false;        	}        	// if a minumum was specified, return false if we're less than the min        	if (isc.isA.Number(validator.min) && value < validator.min) {        		if (!validator.errorMessage) {        			validator.defaultErrorMessage = isc.Validator.mustBeGreaterThan;        		}        		return false;        	}        	return true;        },                // String validators        // ------------------------------------------------------------------------------------                //>@classAttr ValidatorTypes.lengthRange    (validatorDefinition : object : IR)        //  This validator type applies to string values only.  If the value is a string value        //  validation will fail if the strings length falls outside the range specified by         //  <code>validator.max</code> and <code>validator.min</code>.<br>        //  Note that non-string values will always pass validation by this validator type.<br>        // Note that the <code>errorMessage</code> for this validator will be evaluated as        // a dynamicString - text within <code>\${...}</code> will be evaluated as JS code        // when the message is displayed, with <code>max</code> and <code>min</code> available as        // variables mapped to <code>validator.max</code> and <code>validator.min</code>.        // @visibility external        //<        lengthRange : function (item, validator, value) {        	// skip empty fields        	if (value == null || isc.is.emptyString(value)) return true;        	        	// if value null/undefined, or isn't a string, return true        	if (!isc.isA.String(value)) return true;                        // Allow dynamic error messages to be eval'd, with pointers to min and max values            validator.dynamicErrorMessageArguments = {validator:validator,                                                       max:validator.max,                                                       min:validator.min}                	// get the length of the value        	var length = value.length,                maxNumber = validator.max != null ? parseInt(validator.max) : null,                minNumber = validator.min != null ? parseInt(validator.min) : null;                            if (!isc.isA.Number(maxNumber)) maxNumber = null;            if (!isc.isA.Number(minNumber)) minNumber = null;                    	// if a maximum was specified, return false if length is greater than the max        	if (maxNumber != null && length > maxNumber) {        		validator.defaultErrorMessage =                     (maxNumber == minNumber ? isc.Validator.mustBeExactLength                                             : isc.Validator.mustBeShorterThan);                return false;            }        	// if a minumum was specified, return false if length is less than the min        	if (minNumber != null && length < minNumber) {        		validator.defaultErrorMessage =                     (maxNumber == minNumber ? isc.Validator.mustBeExactLength                                            : isc.Validator.mustBeLongerThan);                return false;            }        	return true;        },                //>@classAttr ValidatorTypes.contains   (validatorDefinition : object : IR)        //  Determine whether a string value contains some substring specified via         // <code>validator.substring</code>.        // @visibility external        //<        contains : function (item, validator, value) {        	// skip empty fields        	if (value == null || isc.is.emptyString(value)) return true;        	if (!isc.isA.String(value)) value = isc.iscToLocaleString(value);        	return value.indexOf(validator.substring) > -1;        },                //>@classAttr ValidatorTypes.doesntContain   (validatorDefinition : object : IR)        //  Determine whether a string value does not contain some substring specified via         // <code>validator.substring</code>.        // @visibility external        //<        doesntContain : function (item, validator, value) {        	// skip empty fields        	if (value == null || isc.is.emptyString(value)) return true;        	if (!isc.isA.String(value)) value = isc.iscToLocaleString(value);        	return value.indexOf(validator.substring) == -1;        },                //>@classAttr ValidatorTypes.substringCount   (validatorDefinition : object : IR)        //  Determine whether a string value contains some substring multiple times.        //  The substring to check for is specified via <code>validator.substring</code>.        //  The <code>validator.operator</code> property allows you to specify how to test        //  the number of substring occurrances. Valid values for this property are        //  <code>==</code>, <code>!=</code>, <code>&lt;</code>, <code>&lt;=</code>,        //  <code>&gt;</code>, <code>&gt;=</code>.<br>        //  The number of matches to check for is specified via <code>validator.count</code>.        // @visibility external        //<        substringCount : function (item, validator, value) {        	// skip empty fields        	if (value == null || isc.is.emptyString(value)) return true;        	var substring = validator.substring;        	// get the number of times the value contains the substring and put it into "matchCount"        	for (var index = 0,	matchCount = 0; index < value.length; index++) {        		index = value.indexOf(substring,index);        		if (index > -1) matchCount++;        		else break;        	}        	        	var operator = validator.operator,         		count = validator.count        	;        	if (!operator) operator = "==";        	if (!count) count = 0;        	        	switch (operator) {        		case "==" : return matchCount == count;        		case "!=" : return matchCount != count;        		case "<" : return matchCount < count;        		case "<=" : return matchCount <= count;        		case ">" : return matchCount > count;        		case ">=" : return matchCount >= count;        	}        	        	// otherwise return false        	return false;        },                //>@classAttr ValidatorTypes.regexp (validatorDefinition : object : IR)        //  <code>regexp</code> type validators will determine whether the value specified         //  matches a given regular expression.  The expression should be specified on the        //  <code>validator</code> object as the <code>expression</code> property.        // @visibility external        // @example regularExpression        //<        regexp : function (item, validator, value) {        	// skip empty fields        	if (value == null || isc.is.emptyString(value)) return true;        	        	// get the expression to validate and normalize it to a regExp value        	var expression = validator.expression;        	if (isc.isA.String(expression)) {                expression = new RegExp(expression);            }        	        	// return whether or not the expression matches the value        	return expression.test(value);        },        //>@classAttr ValidatorTypes.mask   (validatorDefinition : object : IR)        //  Validate against a regular expression mask, specified as <code>validator.mask</code>.        //  If validation is successful a transformation can also be specified via the        //  <code>validator.transformTo</code> property. This should be set to a string in the        //  standard format for string replacement via the native JavaScript <code>replace()</code>        //  method.        //          // @visibility external        // @example valueTransform        //<        mask : function (item, validator, value) {        	// skip empty fields        	if (value == null || isc.is.emptyString(value)) return true;        	var mask = validator.mask;        	        	// and convert to a regular expression if it's a string        	if (isc.isA.String(mask)) mask = validator.mask = new RegExp(mask);        	        	// check the value against the mask        	if (!mask.test(value)) {        		return false;        	} else {        		// if it passes the test                		// if they specify a transformTo, transform the item and set the         		//	resultingValue to the transformed value        		if (validator.transformTo) {        			validator.resultingValue = value.replace(mask, validator.transformTo);        		}        	}                	// return that the mask was validated successfully        	return true;        },        // Dates        // ---------------------------------------------------------------------------------------        //>@classAttr ValidatorTypes.dateRange    (validatorDefinition : object : IR)        // Tests whether the value for a date field is within the range specified.        // Range is inclusive, and is specified via <code>validator.min</code> and        // <code>validator.max</code>, which should be dates.<br>        // Note that the <code>errorMessage</code> for this validator will be evaluated as        // a dynamicString - text within <code>\${...}</code> will be evaluated as JS code

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -