Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

--scenario

<pre>A textbox is there. I am allowing user to enter only single dot.

Like 34.56

if 34. (then it will become 34)[which i am taking care of it in onblur]


--Now suppose the user enters a number like 342.53. 
then message will appear "alert("Please enter numeric value!");"
--then i was making the text of textbox to be empty

--But suppose user enterd a big number like  34245.454.
then message will appear "alert("Please enter numeric value!");"
--then the textbox will be empty
--and the user has to retype again the number.

So text which i was making empty i removed that line.
So now user click a number  3465.45.
then message will appear "alert("Please enter numeric value!");"
So now user click a number  3465.45..
then message will appear "alert("Please enter numeric value!");"

But when i click outside the textbox then the number now is 3465.45..


------------------------------------
So what i thought like in onblur function

i will check if it is a valid number or not. If it is not a valid number then I will make the text to be empty


--------------------------------------------
XML
<script type="text/javascript">
        function validate1() {
            var num = document.getElementById("txtid").value;
            if (isNaN(num) == true) {
                alert("Please enter numeric value!");
                return false;
            }
            else
                return true;
        }

   </script>
  <script type="text/javascript">
      function onblurvalidate() {
          var num = document.getElementById("txtid").value;
          if (isNaN(num) == true) {
              document.getElementById("txtid").value = "";
              return false;
          }

            var length = num.length;
            var num1 = num + '';
            var dotindex = num1.indexOf(".");
            if (length == (dotindex+1))
            {
                document.getElementById("txtid").value = num.substring(0, num.length - 1);
            }

      }
    </script>


-----------------------------
But to my amazement i got the result like when i entered a wrong number suppose

34.45.
the message appeared "alert("Please enter numeric value!");"
And the text is empty.

How this is possible. As I have not clicked outside the textbox,So it should not fire. Kindly help me understanding this. 


------------------------------
<asp:TextBox ID="txtid" CssClass="textBox_for6tds" runat="server" onkeyup="javascript:validate1();" onblur="onblurvalidate()">
Posted
Updated 11-Jan-14 0:56am
v3

1 solution

Try to use this sample for your scenario

http://jqueryvalidation.org/number-method/[^]

I had check this url. It's enough for you. But, it's not working in below IE8 version.
 
Share this answer
 
Comments
anurag19289 11-Jan-14 7:17am    
This is cool. But can you explain the above scenario.
Paramaa 11-Jan-14 7:33am    
Actually , the validate1 client method is called and validate the textbox value, It's through right error message to you. You did called validation method through onkeyup client method. isNaN mean check is it not a number.

if (isNaN(num) == true) {

In this line checking the correct value.

if num = 99.63

isNaN(num) result as true;


if num = 99.63.

isNaN(num) result as false;
Paramaa 11-Jan-14 7:43am    
But to my amazement i got the result like when i entered a wrong number suppose

34.45.
the message appeared "alert("Please enter numeric value!");"
And the text is empty.

How this is possible. As I have not clicked outside the textbox,So it should not fire. Kindly help me understanding this.



Atually this alert message came from function validate1() { not in function onblurvalidate() { , please check it is using put the debugger inside two function.
anurag19289 12-Jan-14 8:40am    
yaa the method is declared in on in validate1().

My doubt is "why the textbox became empty ?" which is declared in onblur.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900