[thelist] [JS] Truly checking for empty fields
Carl Edwards
edwards at vitesse.com
Fri Apr 9 12:04:58 CDT 2004
Hello Tom,
I do the same equality check for the empty string but
call a function to remove white space from each end of
the field value first. Usually something like:
.
.
.
if (f.elements[e] && trimws(f.elements[e].value) == "") {
.
.
.
return false
}
-Carl Edwards
> -----Original Message-----
> From: Tom Dell'Aringa [mailto:pixelmech at yahoo.com]
> Sent: Friday, April 09, 2004 7:50 AM
> To: thelist at lists.evolt.org
> Subject: [thelist] [JS] Truly checking for empty fields
>
>
> Along the same lines - I've always wondered about this one. When you
> require a field to not be empty, often you see validations do the
> following:
>
> if(myField.value == ""){
> return false
> }
>
> This is fine for a tab or if the field is truly empty, but if I
> simply enter a space I can bypass this validation. If you aren't then
> validating the *type* of data afterwards, this can sneak through your
> validation.
>
> We have this function available which seems to work:
>
> function requiredField(field) {
> var nonblanks = 0;
> var thisChar;
>
> for (i = 0; i < field.value.length; i++) {
> thisChar = field.value.charCodeAt(i);
>
> // 32 is a blank; 9 is a tab
> if (thisChar != 32 && thisChar != 9)
> nonblanks += 1;
> }
>
> if (nonblanks == 0) {
> return false;
> }
> return true;
> }
>
> I'm not sure how reliable checking for the characer numbers 32 and 9
> is. (Note: No need to support anything prior to NS6 or IE5). But it
> seems to work. And again, I'm wondering if a regex is not a better
> solution here.
>
> Comments?
>
> Tom
>
More information about the thelist
mailing list