Current location: Hot Scripts Forums » General Web Coding » JavaScript » length is null or not an object


length is null or not an object

Reply
  #1 (permalink)  
Old 05-12-04, 12:58 PM
SH6811 SH6811 is offline
New Member
 
Join Date: May 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
length is null or not an object

Help!

What is the proper command in Javascript to return the "length" of number of characters a user inputs into a text box on a form?

I'm trying to do this:

...

var input = FMI2FORM.PhoneNumber.value;

if(input.length > 0)
{
return true;
}

...

...and I get "length is null or not an object" error...what gives, I've seen others do this...

thank you
Reply With Quote
  #2 (permalink)  
Old 05-14-04, 03:27 AM
GO4TF4CE's Avatar
GO4TF4CE GO4TF4CE is offline
Wannabe Coder
 
Join Date: Apr 2004
Posts: 223
Thanks: 0
Thanked 0 Times in 0 Posts
try the following:

<form name="form1" method="post" ACTION="whatever.asp">

<input type="text" name="text1" onchange="javascript:checklength()">

</form>


<SCRIPT>
function checklength()
{

var input = form1.text1.value;

if(input.length > 0)
{
alert('The length is ' + input.length);
}

}
</SCRIPT>

as you havent posted all your code, I just made this example to show the length, you can adapt it as needed.
Reply With Quote
  #3 (permalink)  
Old 05-15-04, 01:54 PM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
The reason to why you get 'length is null or not an object' might be that the script is executed before the textfield has been parsed by the browser and the script can't check something which isn't there.

If you do it the way GO4TF4CE suggested you'll get around the problem easily.
Reply With Quote
  #4 (permalink)  
Old 05-15-04, 03:17 PM
Chris Hill Chris Hill is offline
Newbie Coder
 
Join Date: Mar 2004
Location: UK
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
You could do it in many ways, such as if you wanted form validation, you could have:

<script language="javascript">
<!--

function check(){

if(document.form_name.object_name.value.length == 0){
alert('something');
return false;

other JS commands

}

}

//-->
</script>

<input type="submit" name="submit" onClick="return check();">

You could also have it in the form tag, <form name="something" method="post" action="URL" onSubmit="reurn check();">

}
__________________
<a href="http://www.WhiteHillStudios.co.uk/home.php" target="_blank">White Hill Studios</a> - driving down the cost of web design<br>
<a href="http://www.agscriptz.com" target="_blank">AG Scriptz ii: renaissance</a> - a new dawn or scripting
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
login script cyberted Script Requests 2 05-06-04 01:57 AM
Newbie MySQL fccolon PHP 2 03-16-04 10:54 AM
MySQL Query problem Wraith PHP 5 03-06-04 05:16 PM
picking random entries with a filter... Double selection problem dsumpter PHP 7 11-16-03 07:19 PM
Help with making a installer config script dazz Job Offers & Assistance 3 09-29-03 02:51 AM


All times are GMT -5. The time now is 02:20 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.