473,840 Members | 1,670 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

best way to test blank data in required fields

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.user name.value == '')
alert("username is required");
Jul 23 '05 #1
7 3076

"Matt" <jr********@hot mail.com> wrote in message
news:ba******** *************** ***@posting.goo gle.com...
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.user name.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
On 15 Sep 2004 16:33:26 -0700, Matt <jr********@hot mail.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.user name.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
> In what way doesn't it work? Is it a silent failure, a script error?
if (InputForm.user name.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
On 16 Sep 2004 09:50:07 -0700, Matt <jr********@hot mail.com> wrote:

[snip]
if (InputForm.user name.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.valu e)) {
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
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
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.user name.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="whateve r" method="post" onsubmit="retur n
checkFields(thi s);"> ?

Then
function checkFields(for m){
if(form.usernam e.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
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.val ue;
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3239
by: JDS | last post by:
Hi, all. I'd like to do the following, preferably *without* resorting to JavaScript: I have a long, dynamically-generated form questionnaire. Not all of the form fields are dynamically generated, though. I'd like to capture the NAME of every HTML form field element on the server, even if that element is submitted blank. The trouble is, with, say, radio buttons or checkboxes for example, a *blank* element does not get submitted at all.
2
2940
by: Mike Button | last post by:
Hello all, I am really really desperate on what I should do, and I am asking for help from anyone in this newsgroup, here's the situation: I am creating a form that is being run on a server where there is no scripts allowed running (the software is from Opentext called Livelink)- therefore I need javascript to do the tasks listed below: 1. validate the form - this has been completed 2. pop up another window that will go ahead and...
19
7028
by: Joe Scully | last post by:
Hi all, I am having trouble with access adding a blank record to a subform everytime I finish entering data and closing the form. (The form and subform are based on the one table) When entering data into the subform I get the pencil icon to show data addition, but underneath the record I am inputting data the record is blank with a star(*)icon and this is the record(blank)that always gets saved with all the records that actually have...
1
2838
by: hartley_aaron | last post by:
Hi, I was trying to blank out (null out?) a field of a record that is a date/time data type. Here is code excerpt: Dim D As Database Dim rsRacks As Recordset Dim strSQL As String strSQL = "SELECT Racks.* FROM Racks WHERE (((Racks.)= "
4
1197
by: AMeador | last post by:
I have a form that should come up blank (no data in the text boxes, etc...). I want the user to be able to fill in the contols on the form and then save the data to a SQL Server database. My plan up to this point is to use a dataset and have it bound to the controls in the form. I want to do this so I can take advantage of Undoing changes easily. The problem is I don't know how to deal with the dataset when an initial record hasn't been...
0
4260
by: Anonieko Ramos | last post by:
ASP.NET Forms Authentication Best Practices Dr. Dobb's Journal February 2004 Protecting user information is critical By Douglas Reilly Douglas is the author of Designing Microsoft ASP.NET Applications and owner of Access Microsystems. Doug can be reached at doug@accessmicrosystems.com. --------------------------------------------------------------------------------
7
7002
by: h7qvnk7q001 | last post by:
I'm trying to implement a simple server-side form validation (No Javascript). If the user submits a form with errors, I want to redisplay the same form with the errors highlighted. Once the form is correct I need to submit to another page that uses the form data. I first tried making the form submit action= field point to the same file. When the form was correct, I tried loading the next page by using <META http-equiv refresh>. But...
1
2472
by: Dave | last post by:
I have multiple forms that will create an object. Basically a energy efficiency measure object. The measure object will have a couple of required properties set but after that it can have 10-20 different fields that are optional per measure. How do I account for the different fields that will be posted from the different forms when I create the measure object? Should I create a constructor method with just the required fields as the...
0
2635
by: Pasquale | last post by:
I have a form with 8 fields, 4 of which are required and 4 are not. Usually if a field isn't required I would create a separate table for it rather than having a blank value in the table where required values would go. In this case, I'm not sure if I should put all the not required fields in one table or separate tables. Since any one of them can be or not be blank. I'm trying to find a balance between normalization and practicality for...
0
9861
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9699
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9444
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7023
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5685
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4498
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4078
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3139
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.