Click to See Complete Forum and Search --> : Double onchange
Dan Drillich
09-25-2004, 03:59 PM
Good Day,
I have the following form. The ResultsPerPage field should contain only numbers. It should be enforced by the checkpage JS function invoked via the onchange event.
However, when entering there aa for the first time, the check works and 25 is assigned back. If I retype aa and move to the second field, the check is not invoked.
Any ideas?
BTW, If after the first aa we enter bb, the check will be invoked.
Thanks,
Dan
<SCRIPT LANGUAGE="JavaScript">
// Check that a string contains only numbers
function isNumeric(string) {
if (string.search) {
if (string.search(/\D/) != -1) return false;
}
return true;
}
function checkpage() {
if (!isNumeric(document.asform.ResultsPerPage.value)) {
alert("Results per page must be numeric");
document.asform.ResultsPerPage.value=25;
document.asform.ResultsPerPage.focus();
}
}
</script>
<form action="processmail.php" method="get" name="asform">
<input type="text" name="ResultsPerPage" value="25" onchange="javascript:checkpage()" >
<input type="text" name="temp" value="12">
<input type="submit">
</form>
JPnyc
09-25-2004, 04:03 PM
Call the function again onblur. Also, you have no else for the return true statement. Without it, that code will always execute after the if condition does, provided the test for the if condition passes.
Charles
09-25-2004, 04:06 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<meta name="Content-Style-Type" content="text/css">
<title>Example</title>
<style type="text/css">
<!--
form {width:10em}
fieldset {padding:1ex}
label {display:block; margin:1em 0}
input {display:block; width:9em}
button {display:block; margin:auto}
-->
</style>
</head>
<body>
<form action="someScript.pl">
<fieldset>
<legend>Example</legend>
<label>Results per Page<input value="25" type="text" onchange="if (isNaN (this.value)) {alert (); this.value = '25'; this.focus()}"></label>
<button type="submit">Submit</button>
</fieldset>
</form>
</body>
</html>
Dan Drillich
09-25-2004, 04:28 PM
Charles,
Unfortunately your code behaves in the same way as mine. If I enter in your form aa, I do get the alert message. However, If I enter aa _again_ it's possible to submit the form with the unacceptable aa value.
Thanks,
Dan
JPnyc
09-25-2004, 04:33 PM
Told ya, call the function again, onblur of the textbox.
Charles
09-25-2004, 04:39 PM
Originally posted by Dan Drillich
Charles,
Unfortunately your code behaves in the same way as mine. If I enter in your form aa, I do get the alert message. However, If I enter aa _again_ it's possible to submit the form with the unacceptable aa value.
Thanks,
Dan Then you are making some kind of change to my example. Post a link to what you have.
JPnyc
09-25-2004, 04:48 PM
By the way, any kind of validation should be onsubmit of the form tag. You can do preliminary ones if you like, but the main validation belongs onsubmit of the form tag.
Dan Drillich
09-25-2004, 04:49 PM
DUNSEL,
The onblur does it! It did the trick for Charles' code and for mine.
But why didn't the onchange catch it?
Thank you DUNSEL and Charles,
Dan
Charles
09-25-2004, 04:50 PM
Originally posted by DUNSEL
Told ya, call the function again, onblur of the textbox. I just gave my example another round of testing and it's still working fine.
Charles
09-25-2004, 04:53 PM
Originally posted by Dan Drillich
DUNSEL,
The onblur does it! It did the trick for Charles' code and for mine.
But why didn't the onchange catch it?
Thank you DUNSEL and Charles,
Dan You must be using some browser that incorrectly impliments JavaScript.
JPnyc
09-25-2004, 05:15 PM
I'm pleased it worked, but you really should be validatiing onsubmit event of the form tag.
Charles
09-25-2004, 05:46 PM
Originally posted by DUNSEL
I'm pleased it worked, but you really should be validatiing onsubmit event of the form tag. What is your reason for that claim?
Consider the user. From my own experience and my days as a psych major I've come to believe that people do better with more immediate feedback.
JPnyc
09-25-2004, 06:51 PM
Just the surest,easiest way of keeping the form from submitting when you don't want it to. And you only have to call it once. Like I said, you can do preliminary validations on each field if you like, but I think it's excess.