Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Javascript Form Validation

Posted on 2008-11-11
6
Medium Priority
?
226 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:SilverMecer
  • 4
  • 2
6 Comments
 
LVL 15

Accepted Solution

by:
quincydude earned 525 total points
ID: 22928658
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
 

Author Comment

by:SilverMecer
ID: 22929098
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
 
LVL 15

Expert Comment

by:quincydude
ID: 22929179
Yes sure, just a <input type=hidden name=servervalue value="xxx"> is fine
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:SilverMecer
ID: 22929701
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
 

Author Comment

by:SilverMecer
ID: 22938739
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
 

Author Closing Comment

by:SilverMecer
ID: 31515432
Just needed some adjustments to work with my code.
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question