mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
11b30e8db9
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@82061 467b73ca-7a2a-4603-9d3b-597d59a354a9
60 lines
1.5 KiB
JavaScript
60 lines
1.5 KiB
JavaScript
NumericField = Class.create();
|
|
NumericField.applyTo('input.numeric');
|
|
NumericField.prototype = {
|
|
initialize: function() {
|
|
this.oldValue = this.value;
|
|
},
|
|
|
|
setRange: function( minValue, maxValue ) {
|
|
this.minValue = minValue;
|
|
this.maxValue = maxValue;
|
|
},
|
|
|
|
onkeyup: function() {
|
|
var testValue = this.value;
|
|
|
|
if( testValue == this.oldValue )
|
|
return;
|
|
|
|
var length = this.maxLength;
|
|
this.value = '';
|
|
|
|
var testedOk = true;
|
|
|
|
var regex = new RegExp( '^\\d{0,' + length + '}$' );
|
|
|
|
// check that the value is numeric
|
|
if( !testValue.match( regex ) )
|
|
testedOk = false;
|
|
|
|
if( testedOk && testValue.length > 0 ) {
|
|
|
|
// check that the number is not outside the range
|
|
if( testedOk && typeof this.minValue != 'undefined' && parseInt(testValue) < this.minValue )
|
|
testedOk = false;
|
|
|
|
if( testedOk && typeof this.maxValue != 'undefined' && parseInt(testValue) > this.maxValue )
|
|
testedOk = false;
|
|
|
|
// use any external tests
|
|
if( testedOk && typeof this.externalValidate != 'undefined' && !this.externalValidate( testValue ) )
|
|
testedOk = false;
|
|
|
|
}
|
|
|
|
if( testedOk ) {
|
|
this.oldValue = this.value = testValue;
|
|
|
|
// DEBUG This produces weird javascript-errors, and is not very useable at all
|
|
// DONT MERGE
|
|
/*
|
|
if( this.value.length == this.maxLength && this.nextField )
|
|
this.nextField.focus();
|
|
*/
|
|
|
|
if( this.callOnValidate )
|
|
this.callOnValidate();
|
|
} else
|
|
this.value = this.oldValue;
|
|
}
|
|
} |