On 13 Dec 2004 09:39:35 -0800, <we***********@gmail.com> wrote:
[snip]
The text box should accept only numbers and it should be greater than a
minimum value (this value depends on the user settings). If the user
enters a value less than the minimum value, i should pop up an alert
saying that the value entered is incorrect and reset it back to previous
correct value. [...]
var minimumValue = ...;
/* You might want to rename this to something more descriptive; I
* don't know what it is that you're actually validating, but you
* do.
*
* Include with
*
* <input ... onchange="isValid(this);">
*/
function isValid(input) {
/* Initialise the previousValue property with the initial value
* of the control (specified with the value attribute).
*/
if('undefined' == typeof input.previousValue) {
input.previousValue = input.defaultValue;
}
/* Validate the control value.
*
* The first test ensures that the value only contains integers
* with no leading zeros. The second checks that the number is
* greater-than or equal to the minimum value.
*/
if(!/^(0|[1-9]\d*)$/.test(input.value)
|| (+input.value < minimumValue))
{
alert('Please enter a number greater than ' + minimumValue);
input.value = input.previousValue;
} else {
input.previousValue = input.value;
}
}
Briefly tested.
Hope that helps,
Mike
--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.