6
Answers

Microsoft JScript runtime error: 'document.getElementById(...)' is null or not an object

Ajay Gautam

Ajay Gautam

14y
16.4k
1
Hi,
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;
}
}


<tr>
<th>Username </th>
<td>
<asp:TextBox ID="txtUsernameB" runat="server" CssClass="txtRequired" MaxLength="20" TabIndex="4" AutoPostBack="true" ValidationGroup="vUserDetailsB" Width="243px" ></asp:TextBox>

<asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" ControlToValidate="txtUsernameB" Display="Dynamic" ErrorMessage="Invalid username. Must be between 6 and 20 characters and in non-email format."Font-Size="8pt" SetFocusOnError="True" ValidationExpression="^[a-zA-z][a-zA-Z0-9\.'_-]{4,18}[a-zA-Z0-9]$" ValidationGroup="vUserDetailsB" Width="100%"></asp:RegularExpressionValidator>

<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="txtUsernameB" Display="Dynamic" ErrorMessage="Username is required." Font-Size="8pt" ValidationGroup="vUserDetailsB" Width="100%"> Username is required.</asp:RequiredFieldValidator>

<asp:CustomValidator ID="CustomValidator2" runat="server" Display="Dynamic" ErrorMessage="The username you entered already exists in the system. Please check if you already have an account or specify a different username." Font-Size="8pt" OnServerValidate="CvUsernameServerValidate" ValidationGroup="vUserDetailsB" Width="100%">Username already exists.  Please specify a different username.</asp:CustomValidator>

</td>

</tr>

CS Code

----------

protected void BtnNewAccountByRequestorNext_Click(object sender, EventArgs e)
{
if (RbAccount.SelectedValue.Equals("new"))
{
Page.RegisterStartupScript("anykey", "<script>validateTextBox('"+txtUsernameB.Text+"');</script>");
}

}

Any help appreciated...
Answers (6)