473,385 Members | 1,806 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Validating form fields aren't empty

Hello Everyone,

I am having some trouble getting my JavaScript to validate my form
fields. I've included more information below. My explaination of the
code is in ALL CAPS so it stands out from the code. Can someone tell
me what I am doing wrong?

Thanks!
Anthony
THIS JAVASCRIPT IS IN THE <headSECTION OF MY HTML PAGE. IT IS
SUPPOSED TO VALIDATE THAT THE FORM FIELDS NAMED "uname" AND "pswd" ARE
NOT EMPTY. HOWEVER IT IS NEVER TRIGGERED. PLEASE SEE THE HTML CODE
BELOW THE SCRIPT TO SEE HOW I AM TRYING TO INVOKE IT.

<Script Language="JavaScript">
<!--
function validate(frm){
if(frm.uname.value.length == 0 || frm.pswd.value.length == 0){
alert("You must enter both a username and password to access this
service!");
frm.uname.focus();
return false;
}
-->
</Script>

HERE IS THE FORM DECLARACTION THAT SHOULD TRIGGER IT:

<form name="MyForm" action="signin.php" method="post" onSubmit="return
validate(MyForm)">

I'VE ALSO TRIED THE FORM DECLARATION WITH A ; AT THE END OF THE RETURN
CALL AND IT DIDN'T WORK. ADDITIONALLY, I'VE ATTEMPTED TO INVOKE IT IN
THE SUBMIT BUTTONS onClick EVENT AS:

<input type="submit" value="Sign In" onClick="return
validate(myForm)" />

AND IT DIDN'T WORK EITHER. THE FORM IS ALWAYS SUBMITTED.

Apr 15 '07 #1
4 1775
Anthony P wrote:
Hello Everyone,

I am having some trouble getting my JavaScript to validate my form
fields. I've included more information below. My explaination of the
code is in ALL CAPS so it stands out from the code. Can someone tell
me what I am doing wrong?

Thanks!
Anthony
THIS JAVASCRIPT IS IN THE <headSECTION OF MY HTML PAGE. IT IS
SUPPOSED TO VALIDATE THAT THE FORM FIELDS NAMED "uname" AND "pswd" ARE
NOT EMPTY. HOWEVER IT IS NEVER TRIGGERED. PLEASE SEE THE HTML CODE
BELOW THE SCRIPT TO SEE HOW I AM TRYING TO INVOKE IT.

<Script Language="JavaScript">
<!--
function validate(frm){
if(frm.uname.value.length == 0 || frm.pswd.value.length == 0){
alert("You must enter both a username and password to access this
service!");
frm.uname.focus();
return false;
}
-->
</Script>

HERE IS THE FORM DECLARACTION THAT SHOULD TRIGGER IT:

<form name="MyForm" action="signin.php" method="post" onSubmit="return
validate(MyForm)">
onsubmit="return validate(this)"

Mick
>
I'VE ALSO TRIED THE FORM DECLARATION WITH A ; AT THE END OF THE RETURN
CALL AND IT DIDN'T WORK. ADDITIONALLY, I'VE ATTEMPTED TO INVOKE IT IN
THE SUBMIT BUTTONS onClick EVENT AS:

<input type="submit" value="Sign In" onClick="return
validate(myForm)" />

AND IT DIDN'T WORK EITHER. THE FORM IS ALWAYS SUBMITTED.
Apr 16 '07 #2
onsubmit="return validate(this)"
>
Mick
Nope, still submitting without validation. But thank you anyway for
your answer :-)

Anthony

Apr 16 '07 #3
Changed the function code to this and it worked:

function validate(){

if(document.getElementById('uname').value.length == 0 ||
document.getElementById('pswd').value.length == 0){
alert("You must enter both a username and password to access this
service!");
document.getElementById('uname').focus();
return false;
}
}

Thanks for all who helped.

Apr 16 '07 #4
Anthony P said the following on 4/15/2007 6:56 PM:
Hello Everyone,
My name isn't "Everyone" but Hello to you as well.

<snip>
<form name="MyForm" action="signin.php" method="post" onSubmit="return
validate(MyForm)">
That should give an error as MyForm - in that scenario - is trying to
reference a variable that doesn't exist.

validate(this)

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Apr 16 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Wm | last post by:
I'm having a very hard time trying to add an edited listing to mySQL. I'm breaking this in to two halves, so that each error/problem can be pinpointed. The edit form queries mySQL and fills in...
4
by: Gleep | last post by:
Hi PHP coders, I've got an issue I'm stuck with. Imagine there is a large form that has 5 columns and 20 rows. In each row there is a check box - then 4 input fields. I already have the code...
4
by: Andrew Williams | last post by:
Does anyone know of a way to quickly validate an HTML web form? I need to make sure that certain fields on my form actually contain data and are not submitted blank. I have tried using...
2
by: Tim Mills | last post by:
The following code asks the user to sumbit a name, email address, and some text for a quotation via a FORM. I have written a javascript function to evaluate the fields in the form and pop-up a...
8
by: Shane Jones | last post by:
on my php page i have a submit button and when I submit i want to validate that dome fields have data in. i have this as my submit button <input name="Enter" type="button"...
0
by: dmedina | last post by:
I have a form with about 5 or 8 textboxes and all of them have required field validators... When I submit the form, if some of this field are empty I show the Summary msg telling the user which...
1
by: danavni | last post by:
I have a form where the user need to fill some fields. some of these fields are address fields (street, city, state, zip) i want to validate that atleast one of these fields if filled with data....
3
by: Jennifer.Berube | last post by:
okay...so I got this login script and I edited it all and it seems to run fine...IE it listens to the script as far as permissions go when I place a restriction on a page and when you login it...
2
by: lrheeza | last post by:
Hello everyone, I am a newbie at MS Access and I need help!!! I am importing an excel file using Import functionality in MS Access, all the fields are required but there are instances in the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.