Solved

javascript field undefined not picking up

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…

821 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