Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #32191

Re: form sending undefined

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: form sending undefined
Date 2017-01-17 16:16 +0100
Organization PointedEars Software (PES)
Message-ID <1645793.CQOukoFCf9@PointedEars.de> (permalink)
References <69ednX2-9KdxNODFnZ2dnUU7-cPNnZ2d@westnet.com.au>

Show all headers | View raw


Andrew Poulos wrote:

> 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: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

form sending undefined Andrew Poulos <ap_prog@hotmail.com> - 2017-01-17 16:21 +1100
  Re: form sending undefined JJ <jj4public@vfemail.net> - 2017-01-17 15:15 +0700
  Re: form sending undefined "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-01-17 15:29 +0100
  Re: form sending undefined Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-01-17 16:16 +0100
    Re: form sending undefined Andrew Poulos <ap_prog@hotmail.com> - 2017-01-19 12:13 +1100
      Re: form sending undefined Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-01-19 18:19 +0100
        Re: form sending undefined Andrew Poulos <ap_prog@hotmail.com> - 2017-01-20 07:28 +1100

csiph-web