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)%>"></font></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.length < 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
SilverMecerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

quincydudeCommented:
A simple checking will be like this, but be reminded you have to store the server forex value in the page for later retrival or you may have to use AJAX in real time to obtain it.
function checkValue(serverValue)
{
	if(parseInt(theForm.NewRate.value) > serverValue*1.1 || parseInt(theForm.NewRate.value) < serverValue*0.9 )
	{
		alert("Invalid input Error!!");
	}
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SilverMecerAuthor Commented:
Thank you will try the code now, to make this work I should have a hidden text box where the value from the server is posted to. Let me know if this is correct
0
quincydudeCommented:
Yes sure, just a <input type=hidden name=servervalue value="xxx"> is fine
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

SilverMecerAuthor Commented:
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.
0
SilverMecerAuthor Commented:
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>&nbsp;&nbsp;&nbsp;</td>
      <td><input type="hidden" name="serverValue" value="<%=rsg(5,0)%>"></td>
    </tr>
0
SilverMecerAuthor Commented:
Just needed some adjustments to work with my code.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.