4

I have simple question but I can't fined good solution on the web.

I have this HTML code:

 <form name="Register" action="Register.aspx" method="post" runat="server" style="margin-top: 15px;" onsubmit="return validateProfile(this);" >

And this JavaScript code

function validateProfile(F) {
var G = F.name; 
}

I want somehow to get the form name, but this code just does not working.

wish for help, thanks!

10
  • Sorry, see the updated code Commented May 24, 2013 at 9:41
  • have you tried F.name? Commented May 24, 2013 at 9:41
  • runat="server" ... where else would it run? Commented May 24, 2013 at 9:42
  • Yes, see the updated code pleas Commented May 24, 2013 at 9:42
  • Why not returning return validateProfile("Register"); Commented May 24, 2013 at 9:42

4 Answers 4

5

There you go

function validateProfile(F) {
    alert(F.name); 
    return false;
}

F is already the form, no need to use .Form .

Since a form is an element, you can access its name using .name.

This is defined in the DOM specification here :

name of type DOMString

Names the form.

Demo

Notice how my JSFiddle contains window.validateProfile = validateProfile because I run it after the DOM is ready, if your function is not directly in a script block, chances are you need to do this too.

Sign up to request clarification or add additional context in comments.

16 Comments

F is the only reliable reference to the form element, this doesn't work with the code OP provided.
After your latest edit, it now looks exactly like OP's code :)
@NaveTseva Would you mind creating a fiddle illustrating your issue? My code works (for your original issue), I can't understand this one you're having, likely people won't be able to either unless you provide more information
@NaveTseva See if this solves your issue stackoverflow.com/a/5792306/1348195 . Like I said, I don't use webforms. Honestly, if you're making anything new, I'd seriously consider switching form web forms to MVC, it took me a few weeks to understand, but once I did I became a lot more productive and it was totally worth it. In ASP.NET MVC there are no such surprises
note that the forms .name attribute can be "polluted" by an input with a name of "name". E.g. <FORM NAME="ActualFormName"> <INPUT NAME="name"> then the JavaScript will alert with an object (the input) instead of "ActualFormName". At least as far back as IE8, if you use the forms attribute collection, and index the "name" attribute, then it will actually return the correct name. e.g. F.attributes["name"].value;
|
4

You likely have a control in the form with a name of name. Form controls are made available as properties of the form, using their name or ID as the property name. So in:

<form name="foo" ...>
  <input name="name" ...>

then:

document.forms['foo'].name

returns a reference to the input element, not the value of the form's name property (which reflects the value of the HTML name attribute).

The solution is to not use attribute values for form controls that are the same as standard form element attribute or DOM property names (e.g. do not name form controls "submit" or "reset" as they will overwrite the form's submit and reset methods).

2 Comments

Sadly, this can be unavoidable. e.g. the guy who designed the database used "name" as a field name, and the action script for posting the data back is not accessable (back end code and you are writing user interface) so you pretty much need to have a text input named "name". In modern browsers, form.getAttribute("name") appears to return the actual forms name, but it doesn't in older browsers.
Just found out that form.attributes["name"].value works (returning the forms name, not the input object) as far back as IE 8, and I believe IE7.
-1
  var  theForms = document.getElementsByTagName("form");  
    for(i=0;i<theForms.length;i++)  
    alert(theForms[i].name);  

or if you have only one form then use theForms [0].name directly

Comments

-4

If you use Jquery. You can get the attr by this :

$("form").attr("name"); // Register

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.