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 NairPosted Oct 19, 2010, 4:03 PM
If the validation needs to happen on button click.
Try this, better way instead if using RegisterStartupScript.
Ajay GautamPosted Oct 19, 2010, 1:20 PM
Ajay GautamPosted Oct 19, 2010, 9:42 AM
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
}
Posted Oct 19, 2010, 9:28 AM
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 GautamPosted Oct 19, 2010, 9:06 AM
Thanks for your reply.
I tried both methods, Still its showing same error
Posted Oct 19, 2010, 8:48 AM
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", "");