SilverMecer
asked on
Javascript Form Validation
Hi there
I have a page where I do validation on the input text. The script works fine but I need to add some complex validation to the script. Currently I only check if a value is entered in the text box.
The page get a forex value from the server and list it on the page. The operator can change the forex value as he wish. I need to validate that the new value entered isn't 10% more or less than the value from the server. Here is the code from the page.
The value for the spot rate I get from the server:
<tr>
<td><font face="Verdana">The current Spot Rate excluding margin:</font></td>
<td><font face="Verdana"><input name="SpotRate"><%=rsg(5,0 )%></font> </td>
</tr>
The admin can change the spot rate in this text box:
<tr>
<td><font face="Verdana">New Rate excluding the margin (as a %):</font></td>
<td><font face="Verdana"><input type="text" name="NewRate" size="20" value="<%=rsg(5,0)%>"></fo nt></td>
</tr>
I do the following validation on the spot rate entered:
Now I want to validate the new rate against the old rate so that the new rate isn't 10% higher or lower than the old rate.
function Validator(theForm)
{
if (theForm.NewRate.value == "")
{
alert("Please enter a value for the \"NewRate\" field.");
theForm.NewRate.focus();
return (false);
}
if (theForm.NewRate.value.len gth < 1)
{
alert("Please enter at least 1 characters in the \"NewRate\" field.");
theForm.NewRate.focus();
return (false);
}
var checkOK = "0123456789-.,";
var checkStr = theForm.NewRate.value;
var allValid = true;
var decPoints = 0;
var allNum = "";
for (i = 0; i < checkStr.length; i++)
{
ch = checkStr.charAt(i);
for (j = 0; j < checkOK.length; j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
if (ch == ".")
{
allNum += ".";
decPoints++;
}
else if (ch != ",")
allNum += ch;
}
if (!allValid)
{
alert("Please enter only digit characters in the \"NewRate\" field.");
theForm.NewRate.focus();
return (false);
}
Thank you
I have a page where I do validation on the input text. The script works fine but I need to add some complex validation to the script. Currently I only check if a value is entered in the text box.
The page get a forex value from the server and list it on the page. The operator can change the forex value as he wish. I need to validate that the new value entered isn't 10% more or less than the value from the server. Here is the code from the page.
The value for the spot rate I get from the server:
<tr>
<td><font face="Verdana">The current Spot Rate excluding margin:</font></td>
<td><font face="Verdana"><input name="SpotRate"><%=rsg(5,0
</tr>
The admin can change the spot rate in this text box:
<tr>
<td><font face="Verdana">New Rate excluding the margin (as a %):</font></td>
<td><font face="Verdana"><input type="text" name="NewRate" size="20" value="<%=rsg(5,0)%>"></fo
</tr>
I do the following validation on the spot rate entered:
Now I want to validate the new rate against the old rate so that the new rate isn't 10% higher or lower than the old rate.
function Validator(theForm)
{
if (theForm.NewRate.value == "")
{
alert("Please enter a value for the \"NewRate\" field.");
theForm.NewRate.focus();
return (false);
}
if (theForm.NewRate.value.len
{
alert("Please enter at least 1 characters in the \"NewRate\" field.");
theForm.NewRate.focus();
return (false);
}
var checkOK = "0123456789-.,";
var checkStr = theForm.NewRate.value;
var allValid = true;
var decPoints = 0;
var allNum = "";
for (i = 0; i < checkStr.length; i++)
{
ch = checkStr.charAt(i);
for (j = 0; j < checkOK.length; j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
if (ch == ".")
{
allNum += ".";
decPoints++;
}
else if (ch != ",")
allNum += ch;
}
if (!allValid)
{
alert("Please enter only digit characters in the \"NewRate\" field.");
theForm.NewRate.focus();
return (false);
}
Thank you
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Yes sure, just a <input type=hidden name=servervalue value="xxx"> is fine
ASKER
I tried it but it doesn't work, I just read that parseInt function parses a string and returns an integer. The problem is with the NewRate we have a currency with dollars or euro and cents. parseInt doesn't include the cents when doing validation. Is there another way of doing this.
ASKER
Hi
Thank you for your support the line you gave me just needed some adjustments.
The JavaScript I tried looks like this and works just fine. It will be better if you can get it one line but for now it works fine.
function Validator(theForm)
if (theForm.NewRate.value > theForm.serverValue.value* 1.1)
{
alert("Sorry, rate is more than 10% of the original rate please correct the \"NewRate\" field.");
theForm.NewRate.focus();
return (false);
}
if (theForm.NewRate.value < theForm.serverValue.value* 0.9)
{
alert("Sorry, rate is less than 10% of the original rate please correct the \"NewRate\" field.");
theForm.NewRate.focus();
return (false);
}
<tr>
<td> </td >
<td><input type="hidden" name="serverValue" value="<%=rsg(5,0)%>"></td >
</tr>
Thank you for your support the line you gave me just needed some adjustments.
The JavaScript I tried looks like this and works just fine. It will be better if you can get it one line but for now it works fine.
function Validator(theForm)
if (theForm.NewRate.value > theForm.serverValue.value*
{
alert("Sorry, rate is more than 10% of the original rate please correct the \"NewRate\" field.");
theForm.NewRate.focus();
return (false);
}
if (theForm.NewRate.value < theForm.serverValue.value*
{
alert("Sorry, rate is less than 10% of the original rate please correct the \"NewRate\" field.");
theForm.NewRate.focus();
return (false);
}
<tr>
<td> </td
<td><input type="hidden" name="serverValue" value="<%=rsg(5,0)%>"></td
</tr>
ASKER
Just needed some adjustments to work with my code.
ASKER