Path: csiph.com!2.us.feeder.erje.net!feeder.erje.net!1.eu.feeder.erje.net!weretis.net!feeder4.news.weretis.net!feeder5.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: form sending undefined Date: Tue, 17 Jan 2017 16:16:49 +0100 Organization: PointedEars Software (PES) Lines: 63 Message-ID: <1645793.CQOukoFCf9@PointedEars.de> References: <69ednX2-9KdxNODFnZ2dnUU7-cPNnZ2d@westnet.com.au> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1484666211 26669 eJwFwYEBwDAEBMCVNOExjjz2H6F3dvGBrjCorW351gR6zuO75HJEM4OyKRmNPsH2mmMSAfkBQ7AR1A== (17 Jan 2017 15:16:51 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Tue, 17 Jan 2017 15:16:51 +0000 (UTC) User-Agent: KNode/4.14.2 X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. I have a simple form with two input fields (one of type email and the > other a text area) and when users select Submit I do a simple check to > make sure that both fields have been filled in. For the email field: > > val = h.elements['email'].value.replace(/\s/g,""); \s+ is more efficient. > > if (val == "") { > err = "You need to include your email."; > } if (!/\S/.test(val)) without the assignment before, is more efficient. If your purpose is to trim leading and trailing whitespace as well, you should use var err; var email = h.elements['email'].value; email = (typeof email.trim == "function" ? email.trim() : email.replace(/^\s+|\s+$/g, "")); if (!/\S/.test(email)) { err = "You need to include your e-mail address."; } [The variable identifier should be indicative of the variable value. Also, “e(-)mail” is the name of the technology and the message, not of the address.] See the FAQ. Or polyfill String.prototype.trim() if necessary and just call .trim(). > And on modern browsers the email field is self validates. Do you mean the “pattern” attribute in HTML5? > In all the testing I've done on multiple browsers in multiple > environments it works as expected but about 5% of the emails it > generates have the value > undefined > for both inputs. > > How do the values become undefined? There is no way that this code can cause the “undefined” value. The problem must be in the code that you have not posted. A possibility for an “undefined” value as the result is if you forgot the second argument to String.prototype.replace(). It happened to me recently. -- PointedEars FAQ: | | Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.