By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,442 Members | 1,368 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,442 IT Pros & Developers. It's quick & easy.

best way to test blank data in required fields

P: n/a
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");
Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a

"Matt" <jr********@hotmail.com> wrote in message
news:ba**************************@posting.google.c 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
Jul 23 '05 #2

P: n/a
On 15 Sep 2004 16:33:26 -0700, Matt <jr********@hotmail.com> 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.
Jul 23 '05 #3

P: n/a
> 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
Jul 23 '05 #4

P: n/a
On 16 Sep 2004 09:50:07 -0700, Matt <jr********@hotmail.com> 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.
Jul 23 '05 #5

P: n/a
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...
Jul 23 '05 #6

P: n/a
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

Jul 23 '05 #7

P: n/a
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

Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.