Page 1 of 1
using javascript to get a FORM's Name
Posted: Fri Oct 31, 2003 9:25 am
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
Posted: Fri Oct 31, 2003 9:52 am
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) {
alert('Form name is: ' + formPtr.name);
formPtr.submit();
return true;
}
//-->
</script>
</head>
<body>
<form action="<?php echo $_SERVERї'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.
Posted: Fri Oct 31, 2003 1:16 pm
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>
Posted: Fri Oct 31, 2003 1:53 pm
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
Posted: Sat Nov 01, 2003 11:50 am
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ї"MSIE 5"] != -1){
form_name = document.formsї0].name;
characters = document.eval(form_name).value.length;
eval(form_name).counter.value = characters;
if(characters > 125){
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;
}
}
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;
Posted: Sat Nov 01, 2003 3:16 pm
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)
{
if (your_text.length > 1000)
{
alert ("You have more that 1000 characters in your text!");
}
}
</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

Posted: Mon Nov 03, 2003 7:26 am
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ї0].name;
document.eval(form_name).description.value = desc;
document.form_name.description.value = desc;
how do i syntax it to work???
Kendall
Posted: Mon Nov 03, 2003 8:28 am
by scorphus
You could try this:
Code: Select all
document.formsї0].description.value = desc;
Posted: Mon Nov 03, 2003 12:36 pm
by kendall
scorphus....dat doesnt work?
why did you say that
Kendall
Posted: Mon Nov 03, 2003 2:33 pm
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) {
alert('Form name is: ' + formPtr.name);
formPtr.submit();
return true;
}
//-->
</script>
</head>
<body>
<form action="<?php echo $_SERVERї'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ї0].desc.value = desc;
alert('document.formsї0].desc.value: ' + document.formsї0].desc.value);
//-->
</script>
</body>
</html>
Cheers,
Scorphus.
Posted: Mon Nov 03, 2003 2:56 pm
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
Posted: Mon Nov 03, 2003 7:18 pm
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ї0].length.value = document.formsї0].description.value.length;
alert('Description: ' + document.formsї0].description.value + '\n' +
'Length: ' + document.formsї0].length.value);
if (document.formsї0].description.value.length > 5)
document.formsї0].description.value = document.formsї0].description.value.substr(0, MAX_LEN);
document.formsї0].length.value = document.formsї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.
Posted: Mon Nov 03, 2003 9:14 pm
by Gen-ik
Not sure if this will be any help to you because I lost track of where the topic was going
This function will return TRUE if the form name matches the input.
Code: Select all
<head>
<script language="javascript">
function checkMe(form,name)
{
if( typeof form.name != "undefined" )
{
if( form.name == name ) return true;
return false;
}
}
</script>
</head>
<body>
<form name="testForm">
<input type="text" onfocus="if(checkMe(this.form,"BOB")){alert('correct')}
else{alert('not correct')}"><br>
<input type="text" onfocus="if(checkMe(this.form,"testForm")){alert('correct')}else{alert('not correct')}">
</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.
Posted: Tue Nov 04, 2003 7:51 am
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(){
var desc;
var characters;
if (parseInt(navigator.appVersion) >= 5 || navigator.appVersion.indexOfї"MSIE 5"] != -1){
characters = document.formsї0].description.value.length;
document.formsї0].counter.value = characters;
if(characters > 125){
alert('You exceeded the maximum limit of characters for your description');
desc = document.formsї0].description.value.substr(0,120)+'...';
document.formsї0].description.value = desc;
}
}
}
just incase anyone wants to use and improve on it
Again thanks for your help and sorry about the mistake i made
Kendall