using javascript to get a FORM's Name

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

using javascript to get a FORM's Name

Post by kendall »

Hello,

Can you get a FORM's Name?

i am researching a form.name attribute in javascript but i dont no how it is used
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

It's a string! Since you have a pointer to the form you can access its name. Try:

Code: Select all

<html>
<head>
	<title>.o0o.</title>
	<script language="JavaScript1.2" type="text/javascript">
	<!--
	function foo (formPtr) &#123;
		alert('Form name is: ' + formPtr.name);
		formPtr.submit();
		return true;
	&#125;
	//-->
	</script>
</head>

<body>

<form action="<?php echo $_SERVER&#1111;'PHP_SELF'] ?>" method="POST" name="form1" onSubmit="javascript:foo(this)">
	<input type="text" name="username"><br>
	<input type="password" name="password"><br>
	<input type="text" name="email1" value="me@myhost.com" disabled="true"><br>
	<input type="hidden" name="email2" value="me@myhost.com">
	<input type="submit" name="login" value="Log In">
</form>

<pre>
<?php
print_r($_POST);
?>
</pre>

</body>
</html>
Cheers,
Scorphus.
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post by Cruzado_Mainfrm »

try

Code: Select all

<script language="javascript">
var formname = document.forms[0].name;
//now 'formname' has the name of the first form in the HTML doc
</script>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST" name="form1">
<input type="text" name="username"><br>
<input type="password" name="password"><br>
<input type="text" name="email1" value="me@myhost.com" disabled="true"><br>
<input type="hidden" name="email2" value="me@myhost.com">
<input type="submit" name="login" value="Log In">
</form>
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Post by kendall »

Cruz,

ah ok

i thought i had the wrong syntax i was trying that but i was not getting anything kept getting a error that the form[0].name is not an object or is null

but i get the picture and will work on solving the problem

Kendall
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Post by kendall »

Im trying to get an textarea's character lenght and im trying to dynamically get the forms name....

the foollowing is the code that executes on a KeyDown or Blur

Code: Select all

if (parseInt(navigator.appVersion) >= 5 || navigator.appVersion.indexOf&#1111;"MSIE 5"] != -1)&#123;
		form_name = document.forms&#1111;0].name;
		characters = document.eval(form_name).value.length;
		eval(form_name).counter.value = characters;
		if(characters > 125)&#123;
			alert('You exceeded the maximum limit of characters for your description');
			desc = document.eval(form_name).description.value.substr(0,120)+'...';
			document.eval(form_name).description.value = desc;
		&#125;
	&#125;
However i get and error saying the the method or property is not suppported

what would be the correct syntax for using

document.eval(form_name).description.value = desc;
User avatar
Fredix
Forum Contributor
Posts: 101
Joined: Fri Jul 18, 2003 2:16 pm
Location: Wehr (Eifel) Germany
Contact:

Post by Fredix »

Im trying to get an textarea's character lenght
What about:

Code: Select all

<!-- in the head section -->
<script type="text/javascript">
function get_char_num (your_text)
&#123;
  if (your_text.length > 1000)
  &#123;
     alert ("You have more that 1000 characters in your text!");
  &#125;
&#125;
</script>

<!-- in the body section -->
<textarea onblur="get_char_num(this.value)"></textarea>
Have not tested it but it should work fine and it is an easier solution to that problem in my opinion.

edit: by now I have tested it ;)
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Post by kendall »

Fredix,

Well Fredix i also have a dynamic counter which keeps track of the text lenght as well

I also had the script dynamically reset the text length to the limit any time the length is over the limit.

i jus have a problem in syntax trying to reference the form's name dyanmically

Code: Select all

form_name = document.forms&#1111;0].name;
document.eval(form_name).description.value = desc;
document.form_name.description.value = desc;
how do i syntax it to work???
Kendall
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

You could try this:

Code: Select all

document.forms&#1111;0].description.value = desc;
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Post by kendall »

scorphus....dat doesnt work?

why did you say that

Kendall
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

It works depending on the <form> and on its contents.

Here is another example:

Code: Select all

<html>
<head>
	<title>.o0o.</title>
	<script language="JavaScript1.2" type="text/javascript">
	<!--
	function foo (formPtr) &#123;
		alert('Form name is: ' + formPtr.name);
		formPtr.submit();
		return true;
	&#125;
	//-->
	</script>
</head>

<body>

<form action="<?php echo $_SERVER&#1111;'PHP_SELF'] ?>" method="POST" name="form1" onSubmit="javascript:foo(this)">
	<input type="text" name="username"><br>
	<input type="password" name="password"><br>
	<input type="text" name="desc" value="Description"><br>
	<input type="submit" name="login" value="Log In">
</form>

<pre>
<?php
print_r($_POST);
?>
</pre>

<script language="JavaScript1.2" type="text/javascript">
<!--
var desc = "some data";
document.forms&#1111;0].desc.value = desc;
alert('document.forms&#1111;0].desc.value: ' + document.forms&#1111;0].desc.value);
//-->
</script>

</body>
</html>
Cheers,
Scorphus.
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Post by kendall »

Scrophus,

what do you mean by
It works depending on the <form> and on its contents.
the script is suppose to check the length of characters in the text area so that it doesn't go over the limit i have a input field that dynamically shows the nu,mber of charates inputted. All this happens on an onBlur ot KeyDown

if the character length is more than the maximum size it crops it to the amount it suppose to be.

does this kind of form applies?

Kendall
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

kendall wrote:Scrophus,

what do you mean by
It works depending on the <form> and on its contents.
If you want to update an <input>'s value attribute, then you do document.form[n].%<input>'s name%.value = 'new value'; where 'n' is the number of the form in the order of appearance on the page. So if your form has an <input> named description (<input name="description">) that code works for the purpose of changing the <input>'s value.
kendall wrote:the script is suppose to check the length of characters in the text area so that it doesn't go over the limit i have a input field that dynamically shows the nu,mber of charates inputted. All this happens on an onBlur ot KeyDown

if the character length is more than the maximum size it crops it to the amount it suppose to be.

does this kind of form applies?

Kendall
Yes it does. Take an example:

Code: Select all

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Form Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>

<body>

<form action="some.php" method="post" name="form1">
	Description: <textarea name="description" cols="50" rows="5">testing...</textarea><br />
	Length: <input type="text" name="length" readonly="yes" value="0" />
</form>

<script language="JavaScript1.2" type="text/javascript">
<!--
var MAX_LEN = 5;
document.forms&#1111;0].length.value = document.forms&#1111;0].description.value.length;
alert('Description: ' + document.forms&#1111;0].description.value + '\n' +
      'Length: ' + document.forms&#1111;0].length.value);
if (document.forms&#1111;0].description.value.length > 5)
	document.forms&#1111;0].description.value = document.forms&#1111;0].description.value.substr(0, MAX_LEN);
document.forms&#1111;0].length.value = document.forms&#1111;0].description.value.length;
//-->
</script>


</body>
</html>
This is an illustrative example, so you can see how check the length and crop the content of the text area.

Add this to your existing code that is called by onKeyDown and onBlur.

Also take a look to the JavaScript Central. There you'll find useful information regarding JavaScript.

Hope that helps. Let me know if it doesn't ;)

Cheers,
Scorphus.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Not sure if this will be any help to you because I lost track of where the topic was going :wink:


This function will return TRUE if the form name matches the input.

Code: Select all

<head>
<script language="javascript">

function checkMe(form,name)
&#123;
    if( typeof form.name != "undefined" )
    &#123;
        if( form.name == name ) return true;
        return false;
    &#125;
&#125;

</script>
</head>

<body>

<form name="testForm">
<input type="text" onfocus="if(checkMe(this.form,"BOB"))&#123;alert('correct')&#125;
else&#123;alert('not correct')&#125;"><br>
<input type="text" onfocus="if(checkMe(this.form,"testForm"))&#123;alert('correct')&#125;else&#123;alert('not correct')&#125;">
</form>

</body>
There's loads of different <form> things you can do so if this isn't what you're after just drop me an email at si@urbanchaos.net and I can give you some more help.

Laters.
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Post by kendall »

Gen-ik/ Scrophus,

im really sorry guys...the reason it didnt work was because i kept forgetting to put back the reference to the input's id...it work's fine now

Code: Select all

function countCharacters()&#123;
	var desc;
	var characters;
	if (parseInt(navigator.appVersion) >= 5 || navigator.appVersion.indexOf&#1111;"MSIE 5"] != -1)&#123;
		characters = document.forms&#1111;0].description.value.length;
		document.forms&#1111;0].counter.value = characters;
		if(characters > 125)&#123;
			alert('You exceeded the maximum limit of characters for your description');
			desc = document.forms&#1111;0].description.value.substr(0,120)+'...';
			document.forms&#1111;0].description.value = desc;
		&#125;
	&#125;
&#125;
just incase anyone wants to use and improve on it

Again thanks for your help and sorry about the mistake i made

Kendall
Post Reply