Solved

javascript field undefined not picking up

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Someone recently asked me about how to display a progress indicator on a page while an iframe is loading. And I remember when I first came across this myself. It was a bit tricky to get my head around, but really, it's very simple. The most impor…
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

746 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

14 Experts available now in Live!

Get 1:1 Help Now