Click here to Skip to main content
15,903,203 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hallo

i want a textbox that allow only numbers and another textbox that allow only characters..........

Kindly help me

Kishore R
Gis Solutions
Posted

Function you are looking for

Fore more detail read article : Enhanced Textbox Control[^]

C#
function IntegerAndDecimal(e,obj,isDecimal)
{
    if ([e.keyCode||e.which]==8) //this is to allow backspace
    return true;

    if ([e.keyCode||e.which]==46) //this is to allow decimal point
    {
      if(isDecimal=='true')
      {
        var val = obj.value;
        if(val.indexOf(".") > -1)
        {
            e.returnValue = false;
            return false;
        }
        return true;
      }
      else
      {
        e.returnValue = false;
        return false;
      }
    }

    if ([e.keyCode||e.which] < 48 || [e.keyCode||e.which] > 57)
    e.preventDefault? e.preventDefault() : e.returnValue = false;
}
 
Share this answer
 
Hi,

This Script can be used to validate your form for numeric values. The Numeric TextBox allows only numbers and accepts one decimal Point.

XML
<script language=javascript>
function keyCheck(eventObj, obj)
{
    var keyCode

    // Check For Browser Type
    if (document.all){
        keyCode=eventObj.keyCode
    }
    else{
        keyCode=eventObj.which
    }

    var str=obj.value

    if(keyCode==46){
        if (str.indexOf(".")>0){
            return false
        }
    }

    if((keyCode<48 || keyCode >58)   &&   (keyCode != 46)){ // Allow only integers and decimal points
        return false
    }

    return true
}
</script>


<asp:TextBox ID="txtNumeric" runat="server" onkeypress = "return keyCheck(event, this);"

onpaste = "return false;"></asp:TextBox>
 
Share this answer
 
You can also create a mask for the textbox to accept int values and alphabets only.
 
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