Solved

javascript field undefined not picking up

Posted on 2014-01-29
5
232 Views
Last Modified: 2014-02-02
I have some form fields that I am trying to validate.
I'm not sure that all fields are on the page.
so I tried checking first if the field is defined and then I run the rest of the function.
if(document.getElementById(fieldname)) {
		if(document.getElementById(fieldname).value == "" ) {
			 themessage = (themessage + fieldname+ '\n');
	
			}		}
	}

Open in new window

yet, I am getting the following error in my console
Uncaught TypeError: Cannot read property 'value' of undefined
0
Comment
Question by:rivkamak
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39819133
Where is fieldname coming from? Is that the ID of the input?
Post your whole code.
0
 

Author Comment

by:rivkamak
ID: 39819718
function validate(fieldname, labelname, longname) {
	longname = typeof longname !== 'undefined' ? longname : fieldname;
	if(document.getElementById(fieldname)) {
		if(document.getElementById(fieldname).value == "" ) {
			 themessage = (themessage + longname+ '\n');
	
			clearLabelcss(fieldname,"error-redInput error-redSelect" );
			clearLabelcss(labelname,"error-red" );
			_gaq.push(['_trackEvent', 'form errors', 'onsubmit individual', longname,,true]); 
			ga('send', 'event', 'form errors', 'onsubmit individual', longname,{'nonInteraction': 1});
	
			
		}
	}
}

Open in new window

0
 
LVL 58

Expert Comment

by:Gary
ID: 39819723
...and the html for inputs
0
 

Author Comment

by:rivkamak
ID: 39819730
I use this in the javascript at a later point.

Open in new window


validate('First', 'FirstLabel', 'First Name');

the html looks like this
<label id="FirstLabel" for="First"></label>

<input id="First" type="text" maxlength="50" value="" x-autocompletetype="given-name" title="first name" name="First" style="border-width: 1px; border-style: solid; -moz-border-top-colo…rs: none; -moz-border-left-colors: none; border-image: none;"></input>

Open in new window


Sometimes the field is available and sometimes it isn't.
I need to make sure the script doesn't throw an error if the field is removed.
0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 39819753
With a field
http://jsfiddle.net/GaryC123/3Lf6P/3/

Without a field
http://jsfiddle.net/GaryC123/3Lf6P/1/

Cleaned up JS as it wasn't valid based on what you posted

 
   function validate(fieldname, labelname, longname) {
	themessage="Please enter the ";
    longname = typeof longname !== 'undefined' ? longname : fieldname;
	if(document.getElementById(fieldname)) {
        alert("Field exists")
		if(document.getElementById(fieldname).value == "" ) {
			 themessage +=longname+ '\n';
	
   clearLabelcss(fieldname,"error-redInput error-redSelect" );
   clearLabelcss(labelname,"error-red" );
   		_gaq.push(['_trackEvent', 'form errors', 'onsubmit individual', longname,,true]); 
    		ga('send', 'event', 'form errors', 'onsubmit individual', longname,{'nonInteraction': 1});
	
alert(themessage)				
		}
	}
} 

Open in new window

0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Find out what you should include to make the best professional email signature for your organization.
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question