Click here to Skip to main content
15,907,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i used the following code for textbox validation..


this is the function which i defined in the javascript file.i done this to ensure the textbox should not be null and the textbox should contain only alphabets.
C#
function RegainFocus() 
        { 
            if((document.getElementById("TextBox1").value).length == 0 || 
            (document.getElementById("TextBox2").value).length == 0 ||
            (document.getElementById("TextBox3").value).length == 0||
            !document.getElementById("TextBox1").value.match(/^[a-zA-Z]+$/)||
            !document.getElementById("TextBox2").value.match(/^[a-zA-Z]+$/)||
            !document.getElementById("TextBox3").value.match(/^[a-zA-Z]+$/)) 
             { 
               alert ("please enter a valid data");
             }            
        }

in the form load event i called the javascript file as
C#
Page.RegisterClientScriptBlock("MyScript", "<SCRIPT     Language='JavaScript' src='JScript.js'></SCRIPT>");
Button1.Attributes.Add("OnClick", "RegainFocus()");

this is throwing error for me....."Microsoft JScript runtime error: Object required"

can anyone plss help me in doing this..........???
Posted
Updated 17-Feb-12 2:31am
v3

i.e u are calling the script function while loading page, so u r calling the script function before loading the textboxes. that y its giving the object error.

hence,
add script tag in header tag of aspx page
JavaScript
<head runat="sever">
<SCRIPT Language='JavaScript' src='JScript.js'></SCRIPT>

then u can call this

JavaScript
Button1.Attributes.Add("OnClick", "RegainFocus()");</head>
 
Share this answer
 
v2
Comments
amaljosep 17-Feb-12 8:11am    
i am using a content page....so it does'nt contain head tags and all....so where i can place the script tag in the content page ....
Sanjeev Alamuri 17-Feb-12 8:20am    
place after content tag
<asp:content ContentPlaceHolderID="ContentPlaceHolder" ID="content2" runat="server">
<SCRIPT Language='JavaScript' src='JScript.js'></SCRIPT>
...
...
...
...
// ur design
...

Use FilteredTextBoxExtender of Ajax for validation.
 
Share this answer
 
Use document.getElementById("<%=TextBox1.ClientID%>").value
 
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