473,405 Members | 2,287 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

problem in validating the amount

149 100+
hi,
im my jsp page..i had to enter amount.it is getting validated for nos.But the problem with decimal point...can any one help me..here is the code..
login.jsp
Expand|Select|Wrap|Line Numbers
  1. <s:textfield name="Cost" id="uploadtextContCost" label="Cost "/>
login.js
Expand|Select|Wrap|Line Numbers
  1. if (form.Cost.value == "")
  2.     {
  3.         alert("Enter Content Cost");
  4.         form.Cost.focus();
  5.         return (false);
  6.     }
  7.     var CostValid = document.getElementById('uploadtextContCost').value;
  8.     for(var i = 0; i < CostValid.length; i++)
  9.     {
  10.         currentCode = CostValid.charCodeAt(i);
  11.         if(currentCode!=45 && currentCode!=46 && currentCode<48 || currentCode>57)
  12.         {
  13.             alert('Only Numbers are allowed in Content Cost');
  14.             return false;
  15.         }
  16.         currentCode1 = Math.abs(CostValid);
  17.         if (CostValid != Math.abs(CostValid))
  18.         {
  19.             alert('U had Entered -ve value in Content Cost');
  20.             CostValid = Math.abs(CostValid);
  21.             return false;
  22.         }
  23.     }
thanks in advance,
madhu.
Sep 8 '10 #1
8 1237
Dormilich
8,658 Expert Mod 8TB
it’s far more easy to test, whether the whole content is a valid number.

functions you can use for that (depending on the allowed number type)
- parseInt()
- parseFloat()
- isNaN()
- isFinite()

e.g.
Expand|Select|Wrap|Line Numbers
  1. if (isNaN(CostValid)) {
  2.     alert("input is not a number.");
  3.     return false;
  4. }
Sep 8 '10 #2
madhuriks
149 100+
hi,
i had changed according what u suggested..here is the code..it is getting validated if i enter 12.58.25..but if i enter 12.58..it is not taking..nd the form is not going to another form..can u help me
Expand|Select|Wrap|Line Numbers
  1. <script>
  2.     function test(form) {
  3.  
  4.         if (form.Cost.value == "")
  5.             {
  6.                 alert("Enter Content Cost");
  7.                 form.Cost.focus();
  8.                 return (false);
  9.             }
  10.             var CostValid = document.getElementById('uploadtextContCost').value;
  11.  
  12.             if(!isNaN(CostValid) && CostValid >= 0 && CostValid> 0) {
  13.                 return true;
  14.             } else {
  15.                 alert("you must enter a valid salary");
  16.                 return false;
  17.             }
  18.  
  19.     }
  20. </script>
  21. </head>
  22. <body>
  23. <form action="login.jsp">Cost:<INPUT TYPE="TEXT" NAME="Cost"
  24.     id="uploadtextContCost">Enter reference Number (e.g. F647) <INPUT
  25.     TYPE="button" value="submit" onClick=test(this.form);;></form>
  26. </body>
  27. </html>
Sep 8 '10 #3
Dormilich
8,658 Expert Mod 8TB
well, I wrote
Expand|Select|Wrap|Line Numbers
  1. if (isNaN(…))
not
Expand|Select|Wrap|Line Numbers
  1. if (!isNaN(…))
PS. NaN => Not a Number
Sep 8 '10 #4
madhuriks
149 100+
i need to get as 12.58..if i give single no. it is going to other form..in my form..the cost value can be anything like 1.00 or 0.25..so how to code it..
Sep 8 '10 #5
Dormilich
8,658 Expert Mod 8TB
as I already showed in the example.
Sep 8 '10 #6
madhuriks
149 100+
i tried..if i enter 12.58 it is going to other form..but if i enter 12.58.25 it it also going to other form.
Expand|Select|Wrap|Line Numbers
  1. <script>
  2.     function test(form) {
  3.  
  4.         if (form.Cost.value == "")
  5.             {
  6.                 alert("Enter Content Cost");
  7.                 form.Cost.focus();
  8.                 return (false);
  9.             }
  10.             var CostValid = document.getElementById('uploadtextContCost').value;
  11.  
  12.             if(isNaN(CostValid) && CostValid >= 0 ) {
  13.                 return true;
  14.             } else {
  15.                 alert("you must enter a valid salary");
  16.                 return false;
  17.             }
  18.  
  19.     }
  20. </script>
  21. </head>
  22. <body>
  23. <form action="login.jsp">Cost:<INPUT TYPE="TEXT" NAME="Cost"
  24.     id="uploadtextContCost"><INPUT
  25.     TYPE="button" value="submit" onClick=test(this.form);;></form>
  26. </body>
  27. </html>
Sep 8 '10 #7
Dormilich
8,658 Expert Mod 8TB
that has nothing to do with the validation. that has something to do with event handling.

in DOM 2, you would simply cancel the event action by Event.preventDefault() (though you would have to use the submit event, not the click event). in DOM 0 you must explicitly return false in the event handler (the event handler is the event attribute’s value, not the return value of the called function)

besides that, your logic is screwed. cf. isNaN()

PS. if you don’t exactly know what a (native) JavaScript function does, look it up!
Sep 8 '10 #8
madhuriks
149 100+
ok.i'll check it
Sep 8 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: mangesh | last post by:
Hi, In the following XML, I need to validate the values provided in Order tag using schema. <?xml version="1.0" encoding="UTF-8"?> <Control...
1
by: Peter B. Ensch | last post by:
Can anyone explain why this code fails when the set of checkboxes consists of only one. myForm.update.length returns 0 instead of 1 with the result that I get an alert box even tho' the one...
12
by: Torbjørn Pettersen | last post by:
I'm having problems validating my HTML code because of some ASP code I'm using: ---Start Code--- <% If rs("Average") = 0 Then VotingImage = "<img src="/images/0.gif" alt='No votes yet'>"...
3
by: Blas Croche Solana | last post by:
Hello, I am trying to validate a VXML document against the new vxml 2.0 schema, and I am getting the following errors (and a lot more like these): Reference to undeclared substitution group...
1
by: Mike P | last post by:
I have a web app that has been running happily for months, and on Saturday it suddenly decided that it no longer wanted to validate my XML. Here is my code and sample schema, and example XML : ...
2
by: Mike Bridge | last post by:
Hi- I've created an XHTML extension module which validates correctly using the W3C online schema validator, but fails when I use the .net 1.1 validator. It seems to be choking on an included W3C...
8
by: David C. Stone | last post by:
I'm stuck trying to validate an html 4.01 page that uses an image map. I copied the second client-side example from here: <http://www.w3.org/TR/html401/struct/objects.html#h-13.6> and...
1
mickey0
by: mickey0 | last post by:
hello, validating this file on w3c I have many error: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.