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.