best way to test blank data in required fields

M

Matt

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");
 
H

Hal Rosser

Matt said:
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.
 
M

Michael Winter

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
 
M

Michael Winter

[snip]
[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
 
S

simina

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...
 
A

Antonie C Malan Snr

Matt said:
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
 
A

Antonie C Malan Snr

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top