Click here to Skip to main content
15,901,426 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am using comman JavaScript file for four validation purpose.
Example
JavaScript
//.js file code
function chkBlank(id)
{
    if(id.value == "")
    {
        alert("Please enter the value");
       id.focus();
        return false;
    }
}

HTML
.aspx file 
<![CDATA[<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Login.aspx.vb" Inherits="UI_Login_Login" %>]]>

VB
<script runat="server">
    Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        txtuserid.Attributes.Add("onblur", "check(document.getElementById('txtuserid'));")
    End Sub
</script>


I am getting error, object required. May i know where am I going wrong?
I am new to ASP.NET.

Thanks in advance
Posted
Updated 1-Jun-10 23:43pm
v2
Comments
Sandeep Mewara 2-Jun-10 5:43am    
1. pre tags are your friends for formatting the code part.
2. Dont use text speak like (m, 2, plz,...)

Change attribute line as

txtuserid.Attributes.Add("onblur", "check(this);")
 
Share this answer
 
dhage.prashant01 wrote:
txtuserid.Attributes.Add("onblur", "check(document.getElementById('txtuserid'));")


This is where you are doing wrong.

you are expecting an object of textbox, based on which value you prompt an alert.

Instead use 'this'. it will pass the full textbox object and you can use it the same way as if its selected using document.getElementById

Thus the change would be:
VB
txtuserid.Attributes.Add("onblur", "check(this);")
 
Share this answer
 
thanks guys

I have done something like that

.js file
JavaScript
function chkBlank(objId, lblText)
{
    if(objId.value == "")
    {
        alert("Please enter " + lblText);
        objId.focus();
        return false; 
    }
    else
    {
        return true;
    }
}


.aspx file
XML
<script type="text/javascript" src="../JS/Validation.js"></script>
    <script type="text/javascript" language="javascript">



JavaScript
function Validate()
{
        if(chkBlank(document.getElementById('txtuserid'),document.getElementById('lblUserId').innerText) == false){return false;}
        if(chkBlank(document.getElementById('txtpwd'),document.getElementById('lblPwd').innerText) == false){return false;}
}


.aspx.vb
VB
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        txtuserid.Focus()
        imgblogin.Attributes.Add("onclick", "javascript:return Validate();")
End Sub
 
Share this answer
 
v2
Comments
Ankur\m/ 2-Jun-10 6:41am    
Do you see, how good it looks now. I have put your code snippet inside "Code Block" (PRE tags).
If you don't understand what I mean, compare both versions of your answer.
Hope you follow the simple rules next time. :)
dhage.prashant01 2-Jun-10 6:45am    
tnkss ankur
bt m nw 2 ths site
so cn u guide me hw 2 do
apple blue 4-Jun-15 23:07pm    
What kind of language do you speak? Weird
You've been given good answers, but I'd like to cover why document.getElementById didn't work with txtUserId.

In HTML (as in so many other programming areas), an ID must be unique, and ASP.NET allows you to use the same ID in many different controls that are placed on a screen. This means that there has to be some mechanism that transforms the ID into something unique, and ASP.NET achieves this by mangling the name - this is why there is an property of each control called ClientID - this can be used to find out what the client side ID is.
 
Share this answer
 
Comments
dhage.prashant01 2-Jun-10 7:08am    
he i have been seeing ClientID but i came to know its actual meaning nw
thanks buddy
Pete O'Hanlon 2-Jun-10 7:16am    
You're welcome. Glad to help.

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