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

i have a one problem regarding to the javascript. i use the function which make textbox enter only numbers with decimal point but i can`t get. so please help me to get out of this ..

i use :

var temp=document.getElementById("<%=txt_rate.ClientID%>").value;
 var len=temp.length;
 for(var i=0;i<temp.length;i++)
 {
        var temp1=temp.charAt(i);
        if(isNaN(temp1))
        {
            alert("Please Enter Rate");
            document.getElementById("<%=txt_rate.ClientID%>").focus();
            return false;
         }
 }


i want to make to allow the user to enter the value with numbers as well as dot. so please help me....
Posted
Comments
[no name] 14-Sep-12 7:42am    
Okay do change your if statement to allow a dot. What is the problem?
[no name] 14-Sep-12 7:45am    
how can i ?
[no name] 14-Sep-12 7:50am    
What on earth do you mean "how can i"? Open your file in whatever editor that you are using and you type in the code to change your if statement to allow a dot as well as a number. It's called "programming".
[no name] 14-Sep-12 7:54am    
dear i understand all the possibilities to done this. and i am not get the sucess then and only then i post it. if you do not want to give the answer in straight way, so nothing to worry.. bcz this site is develop for the help purpose in Programming. so whatever you want to do ?
[no name] 14-Sep-12 7:57am    
No obviously you do not know. And I gave you the answer if you are not programmer enough to know the difference between checking a character for being a number and character being a dot then perhaps you are in the wrong line of work.

1 solution

hi dear,

try below function.

Please remember that you have to call this function on keypress event of text box

JavaScript
function ValidDecimalPrice(textBox, evt) {
    // Call This Function On KeyPress Event Of TextBox
    var txtPrice = document.getElementById(textBox);
    var valPrice = txtPrice.value;
    if (navigator.appName.toLowerCase() == "microsoft internet explorer") {
        evt = evt || window.event;
        var charCode = evt.keyCode;
        //alert(charCode);
        if (charCode > 31 && (charCode < 48 || charCode > 57) && (charCode != 46)) {
            event.ValidDecimalPrice ? event.ValidDecimalPrice() : event.returnValue = false;
        }
        if (charCode == 46 && valPrice.indexOf('.') > -1) {
            event.ValidDecimalPrice ? event.ValidDecimalPrice() : event.returnValue = false;
        }
        if (charCode == 46 && valPrice == "") {
            txtPrice.value = "0.";
            event.ValidDecimalPrice ? event.ValidDecimalPrice() : event.returnValue = false;
        }
    }
    else {
        var charCode = (evt.which) ? evt.which : evt.keyCode;
        if (charCode > 31 && (charCode < 48 || charCode > 57) && (charCode != 46)) {
            return false;
        }
        if (charCode == 46 && valPrice.indexOf('.') > -1) {
            return false;
        }
        if (charCode == 46 && valPrice == "") {
            txtPrice.value = "0.";
            return false;
        }
    }
    return true;
} //////////////////////////



Example for call it in C#

C#
myTextBox.Attributes.Add("OnKeyPress", "return ValidDecimalPrice('" + myTextBox.ClientID + "',event);");

enjoy it
 
Share this answer
 

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