Solved

javascript field undefined not picking up

Posted on 2014-01-29
5
213 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

932 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now