problem in validating the amount

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • madhuriks
    New Member
    • Jun 2010
    • 149

    problem in validating the amount

    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
    Code:
    <s:textfield name="Cost" id="uploadtextContCost" label="Cost "/>
    login.js
    Code:
    if (form.Cost.value == "")
        {
            alert("Enter Content Cost");
            form.Cost.focus();
            return (false);
        }
        var CostValid = document.getElementById('uploadtextContCost').value;
        for(var i = 0; i < CostValid.length; i++)
        {
            currentCode = CostValid.charCodeAt(i);
            if(currentCode!=45 && currentCode!=46 && currentCode<48 || currentCode>57)
            {
                alert('Only Numbers are allowed in Content Cost');
                return false;
            }
            currentCode1 = Math.abs(CostValid);
            if (CostValid != Math.abs(CostValid))
            {
                alert('U had Entered -ve value in Content Cost');
                CostValid = Math.abs(CostValid);
                return false;
            }
        }
    thanks in advance,
    madhu.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    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.
    Code:
    if (isNaN(CostValid)) {
    	alert("input is not a number.");
    	return false;
    }

    Comment

    • madhuriks
      New Member
      • Jun 2010
      • 149

      #3
      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
      Code:
      <script>
      	function test(form) {
      
      		if (form.Cost.value == "")
      		    {
      		        alert("Enter Content Cost");
      		        form.Cost.focus();
      		        return (false);
      		    }
      		    var CostValid = document.getElementById('uploadtextContCost').value;
      
      		    if(!isNaN(CostValid) && CostValid >= 0 && CostValid> 0) {
      		        return true;
      		    } else {
      		        alert("you must enter a valid salary");
      		        return false;
      		    }
      
      	}
      </script>
      </head>
      <body>
      <form action="login.jsp">Cost:<INPUT TYPE="TEXT" NAME="Cost"
      	id="uploadtextContCost">Enter reference Number (e.g. F647) <INPUT
      	TYPE="button" value="submit" onClick=test(this.form);;></form>
      </body>
      </html>

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        well, I wrote
        Code:
        if (isNaN(…))
        not
        Code:
        if (!isNaN(…))
        PS. NaN => Not a Number
        Last edited by Dormilich; Sep 8 '10, 11:45 AM.

        Comment

        • madhuriks
          New Member
          • Jun 2010
          • 149

          #5
          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..

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            as I already showed in the example.

            Comment

            • madhuriks
              New Member
              • Jun 2010
              • 149

              #7
              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.
              Code:
              <script>
              	function test(form) {
              
              		if (form.Cost.value == "")
              		    {
              		        alert("Enter Content Cost");
              		        form.Cost.focus();
              		        return (false);
              		    }
              		    var CostValid = document.getElementById('uploadtextContCost').value;
              
              		    if(isNaN(CostValid) && CostValid >= 0 ) {
              		        return true;
              		    } else {
              		        alert("you must enter a valid salary");
              		        return false;
              		    }
              
              	}
              </script>
              </head>
              <body>
              <form action="login.jsp">Cost:<INPUT TYPE="TEXT" NAME="Cost"
              	id="uploadtextContCost"><INPUT
              	TYPE="button" value="submit" onClick=test(this.form);;></form>
              </body>
              </html>

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                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.preventDe fault() (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!
                Last edited by Dormilich; Sep 8 '10, 12:09 PM.

                Comment

                • madhuriks
                  New Member
                  • Jun 2010
                  • 149

                  #9
                  ok.i'll check it

                  Comment

                  Working...