Click here to Skip to main content
15,888,733 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi
I have written a javascript function for validation of numbers in asp.net
I want to use same function to be called on 4 textboxes.
JavaScript
<script type="text/javascript"  language="javascript">
    function Isnumber(txt)   
    {
    var validcharacter="0123456789.";
    var char1;

   var textbox= document.getElementById("txt");
   

    var stext= textbox.value;
    for(i=0; i<=stext.length;i++)
    {
    
    char1= stext.charAt(i);
    
    if(validcharacter.indexOf(char1)==-1 )
    {
    
     alert("please enter only numbers");
     document.getElementById("txt").value="";
     return false;
    }
     
    }
    
    }

ASP.NET
<asp:TextBox ID="txtHRA"  runat="server"
önKeyPress="javascript:Isnumber('<%= txtHRA.ClientID%>')"/>

its not working

txt is the function not able to get textbox id which i have passed while calling function.
I have to use the same function for multiple textbox how can i pass texbox id to the function in javascript
Posted
Updated 26-Sep-12 13:37pm
v3
Comments
Sergey Alexandrovich Kryukov 25-Sep-12 23:12pm    
Why passing the id?
--SA
Amu@184 27-Sep-12 6:13am    
Thanks. Its Working. you are right.
I m learning java script.

Probably, you started programming very recently, but the problem is: JavaScript is in fact very hard to understand, despite the simplicity of typical application code.

Instead of passing the id, you can pass the control object itself. Let me show a simple example, with HTML and JavaScript only:
XML
<html>
    <head>
        <script type="text/javascript">
            function doSomethingWithTextBox(textBox) {
                alert(textBox.value);
            }
        </script>
    </head>
<body>

<input type="text" onkeydown="doSomethingWithTextBox(this)" />
<p>Some other text box:</p>
<input type="text" onkeydown="doSomethingWithTextBox(this)" />

</body>
</html>


As you can see, the required parameter is passed as "this" in the event declaration.

[EDIT]

And when you have different text boxes, the same handler will still distinguish them, because "this" object instances are different.
Problem solved!

—SA
 
Share this answer
 
v3
Comments
Ganesh Nikam 26-Sep-12 2:15am    
5+
Sergey Alexandrovich Kryukov 26-Sep-12 17:24pm    
Thank you, Ganesh.
--SA
enhzflep 26-Sep-12 19:08pm    
+5, perfect.
Sergey Alexandrovich Kryukov 26-Sep-12 19:19pm    
Thank you very much,
--SA
jkirkerx 27-Sep-12 23:36pm    
you did put a lot of effort into that so 5+
on your webform control, it would be something like
attribute.add("onBlur="doSomethingWithTextBox(this); return false;)


Try Sergey's first, I use attribute, didn't know the control would accept javascript events. But wait, he's using a regular html element. so if your using a webcontrol, I think attributes would inject the attribute and do the job.
 
Share this answer
 
v3
// Javascript function

JavaScript
function fnAllowDigitsOnly(key)
{      
    var keycode=(key.keywhich)?key.which:key.keyCode;
    if(keycode<48||keycode>57 || keycode !=8)
    {
        return false;
    }
}

// .cs code for calling javascript function
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
       // for each textbox u should call same function 
       text1.Attributes.Add("onkeypress", "return fnvalidatedigit(event)");
       text2.Attributes.Add("onkeypress", "return fnvalidatedigit(event)");
    }
}
 
Share this answer
 
v2
<script type="text/javascript" language="javascript">
C#
function IsSpacial(txt)   {
           var char1;
           var stext = txt.value;
           keyCode = stext.charCodeAt(0);
           if (((keyCode >= '33') && (keyCode <= '47')) || ((keyCode >= '48') && (keyCode <= '57'))||((keyCode >= '58') && (keyCode <= '64')) || ((keyCode >= '91') && (keyCode <= '96')) || ((keyCode >= '123') && (keyCode <= '183'))) {
               alert("First letter is start with  character in Provider Name.");
          return false;
        }
        else {
            return true;
        }


   }






<asp:textbox id="txt1" runat="server" skinid="TextBoxMandatory" maxlength="50" onkeyup="IsSpacial(this)" xmlns:asp="#unknown">
 
Share this answer
 
v2
#Your code is perfect, just remove double cotes while getting the id:
'''
var textbox= document.getElementById("txt");

change to:

var textbox= document.getElementById(txt);

and call the method as:

önKeyPress="javascript:Isnumber(this.id)"
'''

##One thing: you are calling the method onkeypress which will not check the current input charcter, it will take the previous values like if i have 1234 in the text box and now if i press 5 then the script will calling textbox.value it will take only 1234.
 
Share this answer
 
v3
Comments
Dave Kreskowiak 23-Jun-15 0:37am    
You realize this is a 3 years question right?

Don't reply to them.
XML
<asp:TextBox ID="txtHRA" runat="server"

onKeyPress="javascript:Isnumber(this.id);"/>


or simply use

XML
<asp:TextBox ID="txtHRA" runat="server"

onKeyPress="javascript:Isnumber(this);"/>


by doing this you can directly get textbox object. So, no need to get object from id.
 
Share this answer
 
v2

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