I have one text box and that textbox should contain firstname.lastname.
I am doing Client side validation.
I am getting error Microsoft JScript runtime error: 'document.getElementById(...)' is null or not an object.
ASPX code :-
function validateTextBox(str)
{
alert(str);// Getting proper value
if (str.indexOf(".") == -1)
{
alert("Username should have period(.) between firstName and LastName like FirstName.LastName or FirstNameMI.LastName");
document.getElementById('ctl00_ContentPlaceHolder1_txtUsernameB').focus();
// Here i am getting error...
return false;
}
}
CS Code
----------
protected void BtnNewAccountByRequestorNext_Click(object sender, EventArgs e)
{
if (RbAccount.SelectedValue.Equals("new"))
{
Page.RegisterStartupScript("anykey", "");
}
}
Any help appreciated...

Suthish NairRank #92 · 22.8K reputationPosted Oct 19, 2010, 12:03 PM EDT
If the validation needs to happen on button click.
Try this, better way instead if using RegisterStartupScript.
function validateTextBox(str, id, val) { if (str.indexOf(".") == -1) { alert(str); alert(id); alert(val); alert("Username should have period(.) between firstName and LastName like FirstName.LastName or FirstNameMI.LastName"); document.getElementById(id).focus(); return false; } }protected void Page_Load(object sender, EventArgs e) { String s = String.Format("javascript:validateTextBox('{0}', '{1}', '{2}');", TextBox1.Text, TextBox1.ClientID, RadioButtonList1.SelectedItem.Text); Button1.Attributes.Add("onclick", s); }Posted Oct 19, 2010, 4:48 AM EDT
Method 1:
var txtUsernameID = '<%= txtUsernameB.ClientID %>';
document.getElementById(txtUsernameID).focus();
Method 2:
Try passing the ClientID from the server side ...
function validateTextBox(str,id)
{
//your existing code here.
document.getElementById(id).focus();
}
In the server, call like this,
Page.RegisterStartupScript("anykey", "");
Ajay Gautam84 reputationPosted Oct 19, 2010, 5:06 AM EDT
Thanks for your reply.
I tried both methods, Still its showing same error
Posted Oct 19, 2010, 5:28 AM EDT
I tried this and this is working. Can you check your js code once.
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<script type="text/javascript">
function Validate() {
var txtCtrl = '<%= TextBox1.ClientID %>';
//alert(txtCtrl);
document.getElementById(txtCtrl).focus();
return false;
//document.getElementById(txtCtrl).value = "Murali";
}
script>
<h2>
Welcome to ASP.NET!
h2>
<p>
To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">www.asp.neta>.
<asp:TextBox ID="TextBox1" runat="server">asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="Validate();" />
p>
<p>
You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&clcid=0x409"
title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDNa>.
p>
asp:Content>
Ajay Gautam84 reputationPosted Oct 19, 2010, 5:42 AM EDT
Like this it is working fine, but i need to specify one condition like if my New radiobutton is selected then this should validate otherwise no..
if (RbAccount.SelectedValue.Equals("new"))
{
// code
}
Ajay Gautam84 reputationPosted Oct 19, 2010, 9:20 AM EDT