473,408 Members | 1,754 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Cancel OnChange Event

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.

Jul 23 '05 #1
3 10776
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.
Jul 23 '05 #2
we***********@gmail.com wrote:
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>

Jul 23 '05 #3
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 ?

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Covad | last post by:
Hi all, For some reason my change() function is only called when the page loads. I'd much rather it gets called when the select changes. Here's the code: window.onload = init; function...
2
by: F. Da Costa | last post by:
Hi, I'm trying to call/ mimic an onchange function/ event after a user has clicked a checkbox. In response to which a buncch of other ones need to be checked as well. The caveat is that each of...
2
by: Asit | last post by:
In JavaScripts checks for an onChange event against the value of the textbox at the time of the last onChange event. Since an onChange Event never fired after you changed the text first time ,...
13
by: aundro | last post by:
Hello, I've been looking on the web for a solution to this problem: I create a set of checkboxes, and 2 buttons: - one is labeled "All" - the other is labeled "None" Clicking "All" is...
4
by: Zeebra3 | last post by:
Here goes: I have a web form with several asp:dropdownlists, with which, when selection is changed I want to fire an event defined in some clientside js. The content of the clientside code is...
3
by: jab3 | last post by:
Hello. I"m new to this group, and to JavaScript in general, so please forgive me if I breach local etiquette. I'm trying to implement some client-side 'dynamic' validation on a form. I'm having...
3
by: b_naick | last post by:
I realize that the onChange event for a drop down can be trapped as follows: <select name="myDropDown" onChange="somefunc"> Is it possible to trap the onChange event outside of the select...
21
by: Leena P | last post by:
i want to basically take some information for the product and let the user enter the the material required to make this product 1.first page test.php which takes product code and displays...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.