Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > best way to test blank data in required fields

Reply
Thread Tools

best way to test blank data in required fields

 
 
Matt
Guest
Posts: n/a
 
      09-15-2004
I want to test the blank data in required field. If the user enter
blank data, the following code still not work. But if I test for the
length of a string, it doesn't work also, any ideas?? thanks!!

if (InputForm.username.value == '')
alert("username is required");
 
Reply With Quote
 
 
 
 
Hal Rosser
Guest
Posts: n/a
 
      09-16-2004

"Matt" <> wrote in message
news: om...
> I want to test the blank data in required field. If the user enter
> blank data, the following code still not work. But if I test for the
> length of a string, it doesn't work also, any ideas?? thanks!!
>
> if (InputForm.username.value == '')
> alert("username is required");


You might check for null values as well.
If the DB field is 'not required' it may be null.



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.754 / Virus Database: 504 - Release Date: 9/6/2004


 
Reply With Quote
 
 
 
 
Michael Winter
Guest
Posts: n/a
 
      09-16-2004
On 15 Sep 2004 16:33:26 -0700, Matt <> wrote:

> I want to test the blank data in required field. If the user enter blank
> data, the following code still not work. But if I test for the length of
> a string, it doesn't work also, any ideas?? thanks!!


In what way doesn't it work? Is it a silent failure, a script error?

> if (InputForm.username.value == '')
> alert("username is required");


I'd guess a script error. The FAQ (<URL:http://jibbering.com/faq/>) shows
a better way to access the value of a form control.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
 
Reply With Quote
 
Matt
Guest
Posts: n/a
 
      09-16-2004
> In what way doesn't it work? Is it a silent failure, a script error?
>
> > if (InputForm.username.value == '')
> > alert("username is required");

>
> I'd guess a script error. The FAQ (<URL:http://jibbering.com/faq/>) shows
> a better way to access the value of a form control.
>
> Mike


If the user enter many whitespaces, it won't work
 
Reply With Quote
 
Michael Winter
Guest
Posts: n/a
 
      09-16-2004
On 16 Sep 2004 09:50:07 -0700, Matt <> wrote:

[snip]

>>> if (InputForm.username.value == '')
>>> alert("username is required");


[snip]

> If the user enter many whitespaces, it won't work


Then I doubt the problem here is the script, but your definition of
'working'.

As long as validation fails if the user leaves the field completely blank,
the script works exactly as it should do. The issue here is that it
doesn't do what you *want* it to.

If you want to exclude all whitespace characters, and ensure that there is
at least one non-whitespace character, then use a regular expression:

var form = document.forms['InputForm'],
user = form.elements['username'];

if(!/^\S+$/.test(user.value)) {
alert('Please enter a valid user name.');
}

If you want to ensure a minimum number of non-whitespace characters, then
replace the plus (+) with {n,m}, where n is the minimum number, and m is
the maximum. If you omit the latter, there is no maximum limit. For
example,

/^\S{1,10}$/ 1-10 non-whitespace characters
/^\S{7,}$/ at least seven
/^\S{10}$/ exactly ten

Hope that helps,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
 
Reply With Quote
 
simina
Guest
Posts: n/a
 
      09-16-2004
It could be a minor reason but a big trouble...
So:
1 Check for the name of the field. Is it really usesrname or userName
or whatever...
2 Chech the same field from the database: is it a required field?
'cause if it is, you should maybe check for null, too.
3 Try a verification for "undefined" and test again with an alert
message.
4 Do you call the function properly?

Hope it helps...
 
Reply With Quote
 
Antonie C Malan Snr
Guest
Posts: n/a
 
      09-26-2004
Matt wrote:
> I want to test the blank data in required field. If the user enter
> blank data, the following code still not work. But if I test for the
> length of a string, it doesn't work also, any ideas?? thanks!!
>
> if (InputForm.username.value == '')
> alert("username is required");

Hi Matt,

Generally, your code should work. I would say "InputForm" is not the
handle of your form object.

How did you get to the code above?

<form name="InputForm" action="whatever" method="post" onsubmit="return
checkFields(this);"> ?

Then
function checkFields(form){
if(form.username.value == ""){
alert("Fill in the user name");
return false;
}
return true;
}

You should have a return false; if the field is not filled in.

Chris

 
Reply With Quote
 
Antonie C Malan Snr
Guest
Posts: n/a
 
      09-26-2004
I have an application uploading image files to the server. They have to
be of a specific size. How do I reliably check them on the client side?

I did:

if(form.photo3.value != ""){
var img = form.photo3.value;
var piccie = new Image();
piccie.src = img;
var h = piccie.height;
var w = piccie.width;
if(h > 244 || w > 324){
alert("Your photo 3 is " + w + " pixels wide and " + h + " pixels
high.\n The required dimensions are 320w x 240h. Please scale your
picture");
return false;
}

This sometimes works and sometimes does not. I tried piccie.src =
"file://" + img. This also works unreliably.

Any ideas?

Thanks,

Chris

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
blank CD-R and blank DVD-R not recognized by Vista 64 Ultimate =?Utf-8?B?R3JlZyBLaXJrcGF0cmljaw==?= Windows 64bit 13 11-07-2007 12:23 PM
Python pearls required for iteration across fields of data structure NetHead Python 1 07-24-2007 10:14 AM
Blank Data Fields, How2 Preserve Tbl Columns?, use &nbsp; Robert Kattke XML 0 12-31-2003 08:32 PM
test test test test test test test Computer Support 2 07-02-2003 06:02 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57