Cancel OnChange Event

W

websphereguru

I Have a text box in a jsp that is pre populated. 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. How
could i do it using onchange event of the text box.
 
M

Michael Winter

[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
 
R

RobB

I Have a text box in a jsp that is pre populated. 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. How
could i do it using onchange event of the text box.

Integers? Floats?
How are those 'user settings' implemented?

[tested: ie6win2k, firefox/win2k, saf1.2osx]

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>untitled</title>
<script type="text/javascript">
//<![CDATA[

function init_filter()
{
var f;
if (f = document.getElementById('filter'))
{
f.onkeypress = function()
{
this.prev_value = this.value.replace(/[\D]/g, '').replace(/^0+/,
'');
}
f.onkeyup = function()
{
if (/ /g.test(this.value) || /^0+/.test(this.value))
this.value = this.value.replace(/ /g, '').replace(/^0+/, '');
if (!/^\d*$/.test(this.value))
this.value = this.prev_value || '';
}
f.onchange = function()
{
var minv = 4;
if (this.value && Number(this.value) < minv)
{
alert('Minimum value is: ' + minv + '\n\n...please correct.');
this.value = '';
var obj = this;
setTimeout(function(){obj.focus();}, 50);
}
}
}
}

onload = init_filter;

//]]>
</script>
</head>
<body>
<form>
<input id="filter" type="text" name="filter" value="12" />
</form>
</body>
</html>
 
W

websphereguru

Thanks Mike & Rob,
Both of the scripts work fine. The user settings is in float. Does the
code work for all versions(4+) of IE and NS ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,096
Latest member
ThurmanCre

Latest Threads

Top