Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to do a client-side validation of a form using Javascript.

The validation for each INPUT will however depend on the current values of all other INPUTs. A trimmed down version of the script is:

// e.g. call function on an form INPUT
validateX(form.varx.value);

function validateX(xfv) {
// trimmed down version of function...
        var ux=10;
        // save the current value
        xtemp=xvf;
        // set to possible lower limit
        xfv=ux;
        // do calc based on all form INPUTs
        var y1=tempcalc(form);
        // further calculation to get true upper limit of INPUT based on y1
    }


The problem is that 'tempcalc' seems to use the OLD value of the INPUT, not the one assigned with 'xfv=ux'. As far as I can see the value isn't actually updated until the function returns, so that another function called within the body of the calling function can't see the change.

Any ideas how to get round this?

Cheers,
Steve.
Posted

You are correct, the other function can not see the updated value the way your function is written.

Instead of passing in the value to validateX, pass in the control and manipulate the ctl.value inside validateX.

Here is a simple test case:

XML
<html>
<head>
    <script type="text/javascript">
        function test1(val) {
            alert("Value before assignment " + val);
            val = "fred";
            display();
        }
        function test2(ctl) {
            alert("Value before assignment " + ctl.value);
            ctl.value = "fred";
            display();
        }
        function display() {
            alert("Value after assignment " + form.tb.value);
        }
    </script>
<body>
    <form name="form" method="post"  id="aspnetForm">
        <input id="tb" type="text" value="Initial Value" />
        <input type="button" onclick="test1(form.tb.value);" value="do test1" />
        <input type="button" onclick="test2(form.tb);" value="do test2" />
    </form>
</body>
</html>
 
Share this answer
 
v2
Comments
SteveOH979 5-Oct-10 16:27pm    
Funny, it's so obvious once you point it out! Thanks.
Your function is only passing a string value. Use the control instead.

// e.g. call function on an form INPUT
validateX(form.varx);

function validateX(xfv)
{// trimmed down version of function...
var ux=10;
// save the current value
xtemp=xvf;
// set to possible lower limit
xfv.value=ux;
// do calc based on all form INPUTs
var y1=tempcalc(form);
// further calculation to get true upper limit of INPUT based on y1
}
 
Share this answer
 
Comments
SteveOH979 5-Oct-10 16:29pm    
Thanks for this - don't know why I didn't spot this myself?! ... and it's not even Friday!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900