Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble with an If 1

Status
Not open for further replies.

Dashley

Programmer
Dec 5, 2002
925
0
0
US

I have this JS inherited maxlength function checker in an ASP page. I'm trying to modify it so when the session("programid")= 30 max_text_length = 6000;

I put an if statement in that looks like this

if (session("programid") == '30'){
max_text_length = 6000;
}

but I keep getting and generic error. "Object expected"
on a line number way below the function.

The code below is the working original statement.




Code:
function MaxLengthChecker(e)	{
// max length field checker
var max_text_length = 4000;
var counted_chars = 0;
var src_element = e.srcElement? e.srcElement : e.target;
			
if (src_element != null)	{
	if (src_element.value.length > max_text_length) {
		src_element.value = src_element.value.substring(0, max_text_length);
		window.alert('Maximum Character Length Reached.');					
		counted_chars = max_text_length;
		}
	else 
		counted_chars = max_text_length - src_element.value.length;
	if (src_element.type == 'textarea' && src_element.name != 'cpd7' )		{
		popup(counted_chars + ' Characters Left');
	}
	if (src_element.type == 'textbox')		{
		popup(counted_chars + ' Characters Left');
	}
	return true;
}	else	{
	return false;
}					
}
 
Your problem comes from trying to use an asp session variable as a javascript variable. You need to assign session("programid") to a javascript variable, something like this should do:
Code:
function MaxLengthChecker(e)    {
// max length field checker
[COLOR=blue][b]var jsprogramid = [COLOR=red]<%=session("programid")%>[/color];[/b][/color]
var max_text_length = 4000;
var counted_chars = 0;
var src_element = e.srcElement? e.srcElement : e.target;
            
if (src_element != null)    {
    if (src_element.value.length > max_text_length) {
        src_element.value = src_element.value.substring(0, max_text_length);
        window.alert('Maximum Character Length Reached.');                    
        counted_chars = max_text_length;
        }
    else
        counted_chars = max_text_length - src_element.value.length;
    if (src_element.type == 'textarea' && src_element.name != 'cpd7' )        {
        popup(counted_chars + ' Characters Left');
    }
    if (src_element.type == 'textbox')        {
        popup(counted_chars + ' Characters Left');
    }
    return true;
}    else    {
    return false;
}                    
}
The one colorful line I've added to your function should give you a javascript variable jsprogramid that has the value of the ASP session variable "programid" -- you can then use jsprogramid as you had intended to for validation within the javascript function. Hope this helps, let me know if something is unclear!
 
ecwcee


Wow thanks. It was driving me nuts. I don't do a alot of JS, very limited in fact. I sure felt real dumb not being able to do a simple IF.


Thanks Again

Dan
 
Very happy to help! JS is to me something that resides on the client-side (sort of the same level as html), whereas asp is processed server-side. So sticking the asp <%%> tags into a javascript function in such a way leaves that function fully formed once the page is finished being generated, giving us the ability to assign asp variables into js variables the same way we can "response.write" html tags.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top